Saturday, October 20, 2012

Faking It

The wheelchair project is progressing but one element is more difficult than I had initially anticipated; The ability of the chair to react appropriately to sensor input will require a lot of testing to get the results I want. The test procedure looks like this:
  1. Haul the wheelchair to the test site. Unlike other projects, I can't test it in my living room
  2. Hookup the Arduino, breadboard and wires.
  3. Run and videotape the test.
  4. Disassemble the chair, return home
  5. Find,  fix, and facepalm for typing if (a=b) instead of if (a==b)
  6. Repeat
Naturally, this is not feasible. I need a way to simulate all my sensor data from the computer and feed that information to the Arduino. A CSV file would be a natural way to do this. Simply send the contents of the file through the Serial point and I now have a way to run repeated tests, fiddle with my code, and make it work without having to keep running to the test site.

In other words, I have to fake the sensor inputs. This will take a few steps, and more than one post.

Building the Fake Date File (in CSV)

For my project I have 4 sensors planned at the moment and if things go well I may have 5 or 6. Each line in the CSV file will be in this format (the final comma is important):

   timestamp, brakeSensor, reverseSensor, leftSignal, rightSignal,

The Arduino Code to generate the CSV file from real sensor input is below:

//Global variables

const int NUMCOL = 5;  //How many columns in your CSV file
const char SEP = ',';  //Comma is the separator character
long int sensor[NUMCOL];  //long int because the timestamp could get long quickly
const int DELAY_TIME = 10; //How long between sensor readings

//Input pins

const int TS = 0;       //time stamp, not really a pin, but...
const int brake = A1;   //Arduino Analog Pin 1
const int BRAKE = 1;    //Column 1 in the array
const int reverse = A2;
const int REVERSE = 2;
const int left = A3;
const int LEFT = 3;
const int right = A4;
const int RIGHT = 4;

void setup() {
  Serial.begin(9600);  //Begin a serial connection
}

void loop () {
  
  //Since it's so fast, all readings can all share a single time stamp
  sensor[TS] = millis();  //get timestamp
  sensor[BRAKE] = analogRead(brake);
  sensor[REVERSE] = analogRead(reverse);
  sensor[LEFT] = analogRead(left);
  sensor[RIGHT] = analogRead(right);
  
  //Print to serial monitor
  
  for (int i = 0; i < NUMCOL; i++) {
    Serial.print(sensor[i]);
    Serial.print(SEP);
  }
  
  //Output the \n character to make the CSV easier to read
  
  Serial.print('\n'); 
  delay(DELAY_TIME);
  
} // end loop

To get the data to a CSV file, simply open up the Serial Monitor, and copy and paste to Notepad.  To make the next steps easier, you will need to do the following:

  1. At the very top, include or or two lines of "dummy data". When reading the file, this will give the serial port time to connect on both ends without risk of missing real data. I used two rows of all zeroes.
  2. Indicate the end of the file with an '@' character. I actually used @,@,@,@,@ but only one should be needed.


In the next post I will include the code needed to send the sensor data to the Arduino to allow you to "fake it" with your sensors.

Creative Commons License
Making a Better Wheelchair is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.

No comments:

Post a Comment