Go Down

Topic: issue with touschshield slide comms (showing reading then 0 then reading...etc) (Read 633 times) previous topic - next topic

sti robot

I've been back and forth with this project and have "given up" a number of times...

Now I'm back and have a specific issue that is holding things up.  Communications between the arduino and the touchshield is unreliable with my code.  Currently the sensors that work flash between the current reading and 0.  I'm not sure why.

Here is the background info on the project: http://code.google.com/p/robotmeter/
This version of the code is a suite of bar graphs and print outs of the sensor readings.

Code: [Select]

//code for arduino to use with touchshield and other programs
//pulls data from sensors and reports it across serial based on a handshake

#include <HardwareSensor.h>
#include <Wire.h>

int xval = 4;
int yval = 5;
int zerogy = 512;
int zerogx = 512;

int t1pin = 2;
int t2pin = 3;
int boostPin = 1;

float boost;
float t1;
float t2;
float x;
float y;


void setup(){
 //setup connection to tsshield
 Sensor.begin(19200);
 zeroAccelerometer();
}

void zeroAccelerometer(){
 //zero the accelerometer on startup
 int tempreading = analogRead(yval);
 if ((tempreading < 800) && (tempreading > 200)){
   zerogy = tempreading;
 }
 else {
   zerogy = 512;
 }
 tempreading = analogRead(xval);
 if (true) {
   zerogx = tempreading;
 }
 else {
   zerogx = 512;
 }
}

void loop (){
 //listen for queries from tsshield

 //boost = lookup_boost(analogRead(boostPin));
 boost = lookup_boost(analogRead(boostPin));
 t1 = lookup_temp(analogRead(t1pin));
 t2 = lookup_temp(analogRead(t2pin));
 x = getAccelerometerData(xval);
 y = getAccelerometerData(yval);
 Sensor.print("x", x);
 Sensor.print("y", y);
 Sensor.print("t1", t1);
 Sensor.print("t2", t2);
 Sensor.print("bt", boost);
}


//correctly changed for float values
float lookup_boost(int boost){
 //boost = ( (boost-106000) / 259000 );
 // boost = ( (( boost * 398) / 1000) + 2); //2 is the y intercept
 //398 changed to 378 for slope...because slope was too steep
 float fboost = ( (( (float)boost * 378.0) / 1000.0) - 4.0)/10.0; //divide by 10.0 when adding decimals on the display code
 return fboost;
}

//correctly converted to float values
float lookup_temp(int tval){
 float ftval = (float)tval;
 if (tval < 89){
   return (999.9);
 }
 if (tval > 960){
   return (0.0);
 }
 if ((tval <= 960)&&(tval > 932)){
   return (((ftval-1015.77))/(-1.72));
 }
 if ((tval <= 932)&&(tval > 896)){
   return (((ftval-1042.01))/(-2.26));
 }
 if ((tval <= 896)&&(tval > 851)){
   return (((ftval-1077.38))/(-2.80));
 }
 if ((tval <= 851)&&(tval > 791)){
   return (((ftval-1122.64))/(-3.35));
 }
 if ((tval <= 791)&&(tval > 707)){
   return (((ftval-1175.88))/(-3.88));
 }
 if ((tval <= 707)&&(tval > 624)){
   return (((ftval-1214.41))/(-4.21));
 }
 if ((tval <= 624)&&(tval > 532)){
   return (((ftval-1223.67))/(-4.28));
 }
 if ((tval <= 532)&&(tval > 437)){
   return (((ftval-1186.51))/(-4.05));
 }
 if ((tval <= 437)&&(tval > 364)){
   return (((ftval-1113.49))/(-3.66));
 }
 if ((tval <= 364)&&(tval > 306)){
   return (((ftval-1022.32))/(-3.21));
 }
 if ((tval <= 306)&&(tval > 248)){
   return (((ftval-90.78))/(-2.70));
 }
 if ((tval <= 248)&&(tval > 200)){
   return (((ftval-785.75))/(-2.20));
 }
 if ((tval <= 200)&&(tval > 158)){
   return (((ftval-665.07))/(-1.75));
 }
 if ((tval <= 158)&&(tval > 123)){
   return (((ftval-553.00))/(-1.37));
 }
 if ((tval <= 123)&&(tval > 90)){
   return (((ftval-417.52))/(-.94));
 }
}

//correctly converted to use float values
float getAccelerometerData (int axis){
 int zerog = 512;
 if (axis == 4){
   zerog = zerogx;
 }  
 if (axis == 5){
   zerog = zerogy;
 }

 int rc = analogRead(axis);
 int top =( (zerog - rc) ) ;
 float frtrn = ((float)top/(float)158);  //158Vint jumps are 1g for the ADXL213AE (original accel)
 //154Vint jumps are 1g for the ADXL322 (updated one)
 return frtrn;
}




sti robot

Code: [Select]
#include <HardwareSensor.h>
//writen for the touchshield slide
//draws current/peak and a bar graph for:
//  x
//  y
//  bst
//  IC F
//  Turbo F

//debug notes:
//showing max then 0 then max then 0?? with no sensors plugged in and with sensors pluggedin
//shows value then 0 then value...
////comms issue - getting sent 0 and then getting sent real value (arduino code fix)
////comms issue - packing 0 into the var that is getting sent out (arduino code)
////comms error - between checking which sensor it is and then grabbing that value there is an error
////   but the buffer should aready have that value
//with sensors plugged in:
//boost 35 or 32 (not sure why...suspect missing something or other)
///boost also looks like it is matching the t2 output for some odd reason...even with no sensor present
//x and y both at 0
//t1 and t2 are working in their full range (warm hand test)

float boost = 0;
float temp1 = 0;
float temp2 = 0;
float accelx = 0, accely = 0;
float o_x = 160;
float o_y = 160;
float x_peak_neg = 0;
float x_peak_pos = 0;
float y_peak_neg = 0;
float y_peak_pos = 0;
float peak_boost = 0;
float peak_temp1 = 0;
float peak_temp2 = 0;
float o_boost = 0;
float o_temp1 = 0;
float o_temp2 = 0;

int maxBoost = 25;
int maxT1 = 300;
int maxT2 = 300;

int warnBoost = 21;
int warnT1 = 200;
int warnT2 = 200;

int severeBoost = 23;
int severeT1 = 300;
int severeT2 = 300;

void setup(){
  Sensor.begin(19200);
  background(0); //black background
 
  //draw the rectangles and labels
  //-spacing should be 40 pixels tall for each rectangle
  //-all rects start at 50 and go 2xx pixels widen
  //-all rects are 44 high
  fill(0,0,0);
  stroke(255,0,0);
  rect(50,4,220,42);
  rect(50,52,220,42);
  rect(50,100,220,42);
  rect(50,148,220,42);
  rect(50,196,220,42);
 
  //bar labels
  text("boost",8,22,8);
  text("x",20, 52+20,18);
  text("y",20,100+20,18);
  text("t1",14,148+20,18);
  text("t2",14,196+20,18);
 
}

//get the readings from the arduino
void loop(){
  gettouch(); //update the mouse coordinates
  //add section to reset peaks on touch per area

  //change the below section so that it updates in a nicer/more efficient manner
  if (Sensor.available()){
    int value;
    value = Sensor.read();
    if (strcmp(Sensor.getName(), "x")) { 
        accelx = value/100; //get the sensor value
        if ((accelx > x_peak_pos) && (accelx > 0)){
          x_peak_pos = accelx;
        }
        if ((accelx < x_peak_neg) && (accelx < 0)){
          x_peak_neg = accelx;
        }
        print_values_x();
        //draw_bars_x();
      }
      if (strcmp(Sensor.getName(), "y")) { 
        accely = value/100; //get the sensor value
        if ((accely > y_peak_pos) && (accely > 0)){
          y_peak_pos = accely;
        }
        if ((accely < y_peak_neg) && (accely < 0)){
          y_peak_neg = accely;
        }       
        print_values_y();
        //draw_bars_y();
      }
      if (strcmp(Sensor.getName(), "t1")) { 
        temp1 = value; //get the sensor value
        if (temp1 > peak_temp1){
          peak_temp1 = temp1;
        }
        print_values_t1();
        //draw_bars_t1();
      }
      if (strcmp(Sensor.getName(), "t2")) { 
        temp2 = value; //get the sensor value
        if (temp2 > peak_temp2){
          peak_temp2 = temp2;
        }
        print_values_t2();
        //draw_bars_t2();
      }
      if (strcmp(Sensor.getName(), "bt")){
        boost = value;
        if (boost > peak_boost){
          peak_boost = boost;
        }
        print_values_boost();
        //draw_bars_boost();
      } 
  }
    //for debuging the display without the comms stuff/without sensors
    /*boost = boost + random(2);
    accelx = accelx + .01;
    accely = accely + .01;
    temp1 = temp1 + random(2);
    temp2 = temp2 + random(2); */
   
    //avoid flicker as much as possible by only redrawing when there is a change
    //skip the accel readings because they will jump all over the place anyways?
    o_boost = boost;
    o_temp1 = temp1;
    o_temp2 = temp2;
    o_x = abs(accelx);
    o_y = abs(accely);
}


//TODO split this into many functions
void print_values_boost(){
  fill(0,0,0);
  stroke(0,255,255); //light blue
  //current
  text(boost,274,4+6);
  //peak
  text(peak_boost,274,30);
}

void print_values_x(){
  fill(0,0,0);
  stroke(0,255,255); //light blue
  text(accelx,274,52+6);
  text(x_peak_pos,274,52+20);
  text(x_peak_neg,274,52+30);
}

void print_values_y(){
  fill(0,0,0);
  stroke(0,255,255); //light blue
  text(accely,274,100+6);
  text(y_peak_pos,274,100+20);
  text(y_peak_neg,274,100+30);
}

void print_values_t1(){
  fill(0,0,0);
  stroke(0,255,255); //light blue
  text(temp1,274,148+6);
  text(peak_temp2,274,196+26);
}

void print_values_t2(){
  fill(0,0,0);
  stroke(0,255,255); //light blue
  text(temp2,274,196+6);
  text(peak_temp1,274,148+26); 
}

void draw_bars_boost(){
  float twidth=0;
  //boost
  if(boost != o_boost){
    fill(0,255,0); //green
    stroke(0,255,0);
    if (boost > warnBoost){
      fill(255,255,0);
      stroke(255,255,0);
    }
    if (boost > severeBoost){
      fill(255,0,0);
      stroke(255,0,0);
    }
    twidth=218/maxBoost*boost;
    rect(51,5,twidth,40);
    if (o_boost >= boost){
      stroke(0,0,0);
      fill(0,0,0);
      rect(twidth+1+51,5,218-twidth,40);
    }
  }
}


  //x and y will be displayed as positive only (absolute value) in the bar graph
  //they will be light blue and have no warning/severe values

void draw_bars_x(){
  //x
  float twidth=0;
  fill(0,255,255); //light blue
  stroke(0,255,255);
  twidth = 218/200*abs(accelx*100);
  rect(51,53,twidth,40);
  if(o_x >= abs(accelx)){
    stroke(0,0,0);
    fill(0,0,0);
    rect(twidth+1+51,53,218-twidth-1,40);
  }
}
 
void draw_bars_y(){
  //y
  float twidth=0;
  fill(0,255,255); //light blue
  stroke(0,255,255);
  twidth=218/200*abs(accely*100);
  rect(51,101,twidth,40);
  if (o_y >= abs(accely)){
    stroke(0,0,0);
    fill(0,0,0);
    rect(twidth+1+51,101,218-twidth-1,40);
  }
}
 
void draw_bars_t1(){
  //t1
  float twidth=0;
  if (temp1 != o_temp1){
    fill(0,255,0); //green
    stroke(0,255,0);
    if (temp1 > warnT1){
      fill(255,255,0);
      stroke(255,255,0);
    }
    if (temp1 > severeT1){
      fill(255,0,0);
      stroke(255,0,0);
    }
    twidth = temp1;
    rect(51,149,twidth,40);
    if (o_temp1 >= temp1){
      stroke(0,0,0);
      fill(0,0,0);
      rect(twidth+1+51,149,218-twidth,40);
    }
  }
}

  void draw_bars_t2(){
  //t2
  float twidth=0;
  if (temp2 != o_temp2){
    fill(0,255,0); //green
    stroke(0,255,0);
        if (temp2 > warnT2){
      fill(255,255,0);
      stroke(255,255,0);
    }
    if (temp2 > severeT2){
      fill(255,0,0);
      stroke(255,0,0);
    }
    twidth = temp2;
    rect(51,197,twidth,40);
    if (o_temp2 >= temp2){
      stroke(0,0,0);
      fill(0,0,0);
      rect(twidth+1+51,197,218-twidth,40);
    }
  }
}



sti robot

Wait... Small epiphany here.

Can I send a float through Sensor.print and expect it to reassembled the float on the other end or should I break it up into small chunks and reassemble them on the other end (ala the famous scooter computer)?

sti robot

Ok, so i made the code much more simple and now the arduino just gets the values from the sensors via analog read sends those integers straight to the touchshield.

To debug I'm just displaying the those raw 0-1024 values and i'm getting the following behaviors:

-The values change rapidly between multiple values in a large(ish) range.
-That range seems to propogate through all the sensor with exactly the same values.
-They all show a peak of 912.

So, is addressing sensor values like this not working:
Code: [Select]

if (Sensor.available()){
    int value;
    value = Sensor.read();
    if (strcmp(Sensor.getName(), "x")) { 
        //accelx = getAccelerometerData(value)/100; //get the sensor value
        accelx = value;
        if ((accelx > x_peak_pos) && (accelx > 0)){
          x_peak_pos = accelx;
        }
        if ((accelx < x_peak_neg) && (accelx < 0)){
          x_peak_neg = accelx;
        }
        print_values_x();
        //draw_bars_x();
      }
      if (strcmp(Sensor.getName(), "y")) { 
        //accely = getAccelerometerData(value)/100; //get the sensor value
        accely = value;
        if ((accely > y_peak_pos) && (accely > 0)){
          y_peak_pos = accely;
        }
        if ((accely < y_peak_neg) && (accely < 0)){
          y_peak_neg = accely;
        }       
        print_values_y();
        //draw_bars_y();
      }
      if (strcmp(Sensor.getName(), "t1")) { 
        //temp1 = lookup_temp(value); //get the sensor value
        temp1 = value;
        if (temp1 > peak_temp1){
          peak_temp1 = temp1;
        }
        print_values_t1();
        //draw_bars_t1();
      }
      if (strcmp(Sensor.getName(), "t2")) { 
        //temp2 = lookup_temp(value); //get the sensor value
        temp2=value;
        if (temp2 > peak_temp2){
          peak_temp2 = temp2;
        }
        print_values_t2();
        //draw_bars_t2();
      }
      if (strcmp(Sensor.getName(), "bt")){
        //boost = lookup_boost(value);
        boost=value;
        if (boost > peak_boost){
          peak_boost = boost;
        }
        print_values_boost();
        //draw_bars_boost();
      } 
  }

Go Up