Variables aren't available to change until too late

This forum is for all topics related to Simulator for Arduino v0.99. Please include sample code where possible.

Moderator: Adrian

paynterf
Master Class
Posts: 10
Joined: Thu Feb 05, 2015 7:22 am

Variables aren't available to change until too late

Postby paynterf » Thu Feb 05, 2015 7:44 am

Code: Select all

//Step1: Determine if we are closer to left or right walls & adjust speed accordingly
   //01/21/15 now using NewPing class
   //01/26/15 rev to use median distance function
   int leftdistval = LeftPing.ping_cm();
   int rightdistval = RightPing.ping_cm();

   //01/21/15 bugfix: don't adjust at all if both distances are == 0
   if( leftdistval * rightdistval == 0)
   {
      return;
   }


In this code, if a breakpoint is place on the line that defines/sets 'rightdistval', that line is executed when the breakpoint occurs, but the 'rightdistval' variable isn't available for edit in the 'Variables' pane. This is a problem for my code because the very next statement relies on that value for proper operation.

I believe either the line containing the breakpoint should not execute, or the variables pane should show all defined variables (I have attached a screenshot showing the problem). As a workaround, I can probably define the variable on one line and set it on the next - I'll see.

Regards,

Frank
You do not have the required permissions to view the files attached to this post.

Simulator_admin
Site Admin
Posts: 237
Joined: Thu Feb 02, 2012 6:07 pm

Re: Variables aren't available to change until too late

Postby Simulator_admin » Thu Feb 05, 2015 12:22 pm

Hi, this one is a tricky one. There are two points we are looking into - breakpoint execution and setting variables.

With the variables, when the Simulator comes across a line such as int a = 2*12;, the Simulator will first evaluate 2*12=24 and then set this to a. This sounds OK, but when a line such as int a = multiply(2,12); is used, the Simulator will first jump to the multiply routine find out what the return value is, and then substitute this back into the original line. We have done a lot of work or this.

With breakpoints, after each line is executed, the breakpoint is checked to see if the program should stop. Since the program counter is set to a new line after each line, the breakpoint should activate before the next line is executed. This should happen in theory but we did notice once this didn't happen, and this could be due to various conditions.

So to answer the question, we understand the variable not appearing yet,but not sure about the breakpoint. Can you post or email a more complete sketch and we can look into this a bit more. Thanks

paynterf
Master Class
Posts: 10
Joined: Thu Feb 05, 2015 7:22 am

Re: Variables aren't available to change until too late

Postby paynterf » Sat Feb 07, 2015 1:43 pm

Hi,

I have attached my sketch, but I think any sketch will do. Although I certainly can't say with absolute certainty, I have found so far that if I set a breakpoint on a line, whatever code is on that line will get executed before the program halts. For instance, if the line on which the breakpoint is located sets a variable to the result of a function call, then the function will get called and the variable will be updated with the results. I'm OK with this, as whether the program starts before a line executes or after is somewhat arbitrary IMHO.

However, in the case above, if the line that assigns the result of a function to a variable also contains the definition for that varible, i.e. something like

int i = foo(5);

then variable 'i' won't be visible in the variables window until *after the breakpoint is passed* even though the line containing the definition has already executed. I think this only happens when the variable definition AND the assignment are on the same line.

Hope this helps,

Frank

Code: Select all

//01/16/15 - This version of the wall-following robot uses the NewPing
//          Library (C++ class) for ultrasonic sensor management

#include <NewPing.h> //added 01/15/15


bool bStuck = false;
const double STUCK_THRESHOLD_CM = 1; //centimeters
const int MOTOR_SPEED_FULL = 200; //range is 0-255
const int MOTOR_SPEED_HALF = 127; //range is 0-255
const int MOTOR_SPEED_LOW = 50; //added 01/22/15
//const int MOTOR_SPEED_LOW = 10; //added 01/22/15
const int MOTOR_SPEED_OFF = 0; //range is 0-255
//const int LEFT_SPEED_COMP_VALUE = 10; //added 01/14/15 for l/r speed comp
//const int RIGHT_SPEED_COMP_VALUE = -10; //added 01/14/15 for l/r speed comp
const int LEFT_SPEED_COMP_VALUE = -10; //added 01/14/15 for l/r speed comp
const int RIGHT_SPEED_COMP_VALUE = 7; //added 01/14/15 for l/r speed comp
//const int DriveWheelWallFollowTweakVal = 5; //01/24/15 rev to use absolute vs pct
//const int DriveWheelWallFollowTweakVal = 10; //01/24/15 rev to use absolute vs pct
//const int DriveWheelWallFollowTweakVal = 20; //01/24/15 rev to use absolute vs pct
//const int DriveWheelWallFollowTweakVal = 25; //01/24/15 higher vals give better wall following
//const int DriveWheelWallFollowTweakVal = 30; //01/24/15 rev to use absolute vs pct
//const int DriveWheelWallFollowTweakVal = 40; //01/24/15 rev to use absolute vs pct
const int DriveWheelWallFollowTweakVal = 50; //01/24/15 higher vals give better wall following
//const int DriveWheelWallFollowTweakVal = 100; //01/24/15 higher vals give better wall following

//01/10/15 now using Solarbotics motor controller
//left motor pins
const int LeftMotorDirPin = 2;
const int LeftMotorSpeedPin = 3;
const int LeftMotorEnablePin = 4;

//right motor pins
const int RightMotorDirPin = 5;
const int RightMotorSpeedPin = 6;
const int RightMotorEnablePin = 7;

//Ultrasonic sensor pins
const int LeftSensorTrigPin = A0; //uses 'analog' pin (A0 is alias for actual pin number)
const int LeftSensorEchoPin = A1;  //uses 'analog' pin (A1 is alias for actual pin number)
const int FrontSensorTrigPin = 8;
const int FrontSensorEchoPin = 9;
const int RightSensorTrigPin = A4; //uses 'analog' pin (A4 is alias for actual pin number)
const int RightSensorEchoPin = A5; //uses 'analog' pin (A5 is alias for actual pin number)
const int MAX_DISTANCE_CM = 200;
const int MIN_DISTANCE_CM = 2;

//drive wheel parameters
const double DriveWheelRotationSpdRPM = 10.0; //at MOTOR_SPEED_FULL
const double DriveWheelDiameterMM = 66.0;
const double DriveWheelSpdMMPerSec = PI * DriveWheelDiameterMM * DriveWheelRotationSpdRPM / 60;
const double DriveWheelDegPerSec = 360*DriveWheelRotationSpdRPM / 60.0;

//added 12/26/14 for wall following support
int prevleftdistval = 0;
int prevrightdistval = 0;
int prevfrontdistval = 0;

//added 01/16/15 for NewPing support
NewPing LeftPing(LeftSensorTrigPin, LeftSensorEchoPin, MAX_DISTANCE_CM);
NewPing RightPing(RightSensorTrigPin, RightSensorEchoPin, MAX_DISTANCE_CM);
NewPing FrontPing(FrontSensorTrigPin, FrontSensorEchoPin, MAX_DISTANCE_CM);

//added 01/22/15 for speed adjust display LEDs
//01/24/15 direction change
const int rightGRNpin = 12;
const int rightREDpin = 13;
const int leftGRNpin = 10;
const int leftREDpin = 11;

void setup()
{
    //Set OUTPUT pins to drive the left wheel motor
    pinMode(LeftMotorSpeedPin,OUTPUT);
    pinMode(LeftMotorDirPin,OUTPUT);
    pinMode(LeftMotorEnablePin,OUTPUT);

    //Set OUTPUT pins to drive the right wheel motor
    pinMode(RightMotorSpeedPin,OUTPUT);
    pinMode(RightMotorDirPin,OUTPUT);
    pinMode(RightMotorEnablePin,OUTPUT);

    //Setup forward HC-SRS04 ultrasonic sensor
    pinMode(FrontSensorTrigPin,OUTPUT);
    pinMode(FrontSensorEchoPin,INPUT);

    //Setup forward HC-SRS04 ultrasonic sensor
    pinMode(LeftSensorTrigPin,OUTPUT);
    pinMode(LeftSensorEchoPin,INPUT);

    //Setup forward HC-SRS04 ultrasonic sensor
    pinMode(RightSensorTrigPin,OUTPUT);
    pinMode(RightSensorEchoPin,INPUT);

   //01/22/15 set up LED pins as outputs
    pinMode(rightGRNpin,OUTPUT);
    pinMode(rightREDpin,OUTPUT);
    pinMode(leftGRNpin,OUTPUT);
    pinMode(leftREDpin,OUTPUT);

//DEBUG!!
   digitalWrite(rightGRNpin, HIGH);
   digitalWrite(rightREDpin, HIGH);
   digitalWrite(leftGRNpin, HIGH);
   digitalWrite(leftREDpin, HIGH);

   //turn on all 4 in sequence
   int blinkdelay = 500;
   digitalWrite(leftGRNpin, LOW);
   delay(blinkdelay);
   digitalWrite(leftREDpin, LOW);
   delay(blinkdelay);
   digitalWrite(rightGRNpin, LOW);
   delay(blinkdelay);
   digitalWrite(rightREDpin, LOW);
   delay(blinkdelay);

   //turn them all off again in sequence
   digitalWrite(rightREDpin, HIGH);
   delay(blinkdelay);
   digitalWrite(rightGRNpin, HIGH);
   delay(blinkdelay);
   digitalWrite(leftREDpin, HIGH);
   delay(blinkdelay);
   digitalWrite(leftGRNpin, HIGH);
//DEBUG!!

   
   //get initial distance readings
   //01/21/15 now using NewPing class
   //prevfrontdistval = FrontPing.ping_cm();
   //prevleftdistval = LeftPing.ping_cm();
   //prevrightdistval = RightPing.ping_cm();

   //01/26/15 rev to use median distance measurment
   prevfrontdistval = FrontPing.ping_cm();
   prevleftdistval = GetLeftMedianDistCm();
   prevrightdistval = GetRightMedianDistCm();

   //01/26/15 start the robot going straight
   MoveAhead( MOTOR_SPEED_LOW, MOTOR_SPEED_LOW );
}

void loop()
{
//Step 1: Move ahead until an obstruction is encountered
    MoveAheadTilStuck();

//Step 2: Store some information (nothing here yet)
   StoreDistInfo();

//Step 3: back up & turn 90 deg
    RecoverFromStuck();
}

void MoveAheadTilStuck()
{
   //Purpose:  Move the robot forward until it hits an obstruction
   //Provenance: G. Frank Paynter and Danny Frank 12/24/2014
   //Inputs:  None
   //Outputs: Function returns when stuck condition detected
   //Plan:
   //   Step 1: Get current distance to obstacle
   //   Step 2: Move forward 1 second
   //   Step 3: Get a new distance
   //   Step 4: Compare distances - if same, return.  Otherwise continue
   //Notes:
   //   12/26/14 rev to accomodate wall-hugging
   //   01/13/15 rev to use times in msec vs sec
   //  01/24/15 rev to use continuous vs timed motor run

   double deltaD = 0;
   double olddist = 0;
   double newdist = 0;
   bool bStuck = false;
   int leftspeednum = MOTOR_SPEED_HALF;
   int rightspeednum = MOTOR_SPEED_HALF;

   //Step 1: Get current distance to obstacle
   //01/21/15 now using NewPing class
   olddist = FrontPing.ping_cm();

   while(bStuck != true)
   {
   //12/26/14 aded to calculate l/r motor speeds
      UpdateWallFollowMotorSpeeds(leftspeednum, rightspeednum);

   ////Step 2: Move forward 1 second
   //   //01/24/15 now continuous run - no timing
      MoveAhead(leftspeednum, rightspeednum);

   //Step 3: Get a new distance
      //01/21/16 now using NewPing class
      newdist = FrontPing.ping_cm();

   //Step 4: Compare distances - if same, we're stuck.  Otherwise continue
      deltaD = newdist - olddist;
      olddist = newdist; //added 12/26/14
      //bStuck = (abs(deltaD) < STUCK_THRESHOLD_CM);

////DEBUG!!
//      Serial.print("bStuck = ");
//      Serial.println(bStuck);
      bStuck = false;
////DEBUG!!
   }
}


//12/26/14 added for independent l/r speed control
//01/13/15 times now in msec vs sec
//01/24/15 removed time from MoveAhead sig, added MoveAheadMsec
void MoveAhead( int leftspeednum, int rightspeednum )
{
   //Purpose:  Move ahead continuously
   //Provenance: G. Frank Paynter and Danny Frank 01/24/2014
   //Inputs: 
   //   leftspeednum = integer denoting speed (0=stop, 255 = full speed)
   //   rightspeednum = integer denoting speed (0=stop, 255 = full speed)
   //Outputs: both drive motors energized with the specified speed
   //Plan:
   //   Step 1: Set forward direction for both wheels
   //   Step 2: Run both motors at specified speeds

//Step 1: Set forward direction for both wheels
   SetLeftMotorFwdDir(true);
   SetRightMotorFwdDir(true);

//Step 2: Run both motors for timsec seconds
   //01/24/15 - removed timing - now continuous run
   RunBothMotors(leftspeednum, rightspeednum);
}
void MoveAheadMsec( int timeMsec, int leftspeednum, int rightspeednum )
{
   //Purpose:  Move ahead for timesec seconds
   //Provenance: G. Frank Paynter and Danny Frank 12/24/2014
   //Inputs: 
   //   timesec = duration of movement in msec
   //   leftspeednum = integer denoting speed (0=stop, 255 = full speed)
   //   rightspeednum = integer denoting speed (0=stop, 255 = full speed)
   //Outputs: both drive wheels energized for timesec seconds
   //Plan:
   //   Step 1: Set forward direction for both wheels
   //   Step 2: Run both motors for timsec seconds

//Step 1: Set forward direction for both wheels
   SetLeftMotorFwdDir(true);
   SetRightMotorFwdDir(true);

//Step 2: Run both motors for timsec seconds
   RunBothMotorsMsec(timeMsec,leftspeednum, rightspeednum);
}

void RecoverFromStuck()
{
   //Purpose:  Recover from stuck condtion
   //Provenance: G. Frank Paynter and Danny Frank 12/24/2014
   //Inputs: None
   //Outputs: robot backs up slightly and then turns 90 degrees cw
   //Plan:
   //    Step 2: Back up slightly
   //    Step 3: Rotate clockwise 90 degrees

//DEBUG!!
    Serial.println("In RecoverFromStuck()");
//DEBUG!!

//Step 2: Back up slightly
   SetLeftMotorFwdDir(false);
   SetRightMotorFwdDir(false);

   //01/24/15 now have separate functions for continuous vs timed runs
   RunBothMotorsMsec(1000,MOTOR_SPEED_HALF,MOTOR_SPEED_HALF); //01/13/15 time now in Msec

//Step 3: Rotate clockwise 90 degrees
   RotateCWDeg(true,90);
}

void SetLeftMotorFwdDir(bool bFwd)
{
   //Purpose:  Set left motor direction
   //Provenance: G. Frank Paynter and Danny Frank 12/24/2014
   //Inputs: bFwd = true for forward direction, false for reverse
   //Outputs: left motor direction set
   //Plan:
   //    Step 1: Set direction pins appropriately
   //Notes:
   //   01/10/15 now using Solarbotics controller
   //   01/23/15 reversed direction - now 'forward' is toward the castering wheel

//Step 1: Set direction pins appropriately
   //if( bFwd)
   if(!bFwd)//01/23/15 reversed direction - now 'forward' is toward the castering wheel
   {
      digitalWrite(LeftMotorDirPin,HIGH);
   }
   else
   {
      digitalWrite(LeftMotorDirPin,LOW);
   }
}

void SetRightMotorFwdDir(bool bFwd)
{
   //Purpose:  Set right motor direction
   //Provenance: G. Frank Paynter and Danny Frank 12/24/2014
   //Inputs: bFwd = true for forward direction, false for reverse
   //Outputs: right motor direction set
   //Plan:
   //    Step 1: Set direction pins appropriately
   //Notes:
   //   01/10/15 now using Solarbotics controller
   //   01/23/15 reversed direction - now 'forward' is toward the castering wheel

//Step 1: Set direction pins appropriately
   //if( bFwd)
   if(!bFwd)//01/23/15 reversed direction - now 'forward' is toward the castering wheel
   {
      digitalWrite(RightMotorDirPin,HIGH);
   }
   else
   {
      digitalWrite(RightMotorDirPin,LOW);
   }
}


//12/26/14 added for independent motor speed
//01/13/15 rev to use time im Msec vs sec
//01/24/15 time removed from sig (now use RunBothMotorsMsec() for timed runs)
//01/25/15 chg sig to make vars into refs vs copies
//void RunBothMotors(int leftspeednum, int rightspeednum)
void RunBothMotors(int &leftspeednum, int &rightspeednum)
{
   //Purpose: Run both motors at left/rightspeednum speeds
   //Inputs:
   //   speednum = speed value (0 = OFF, 255 = full speed)
   //Outputs: Both motors run for timesec seconds at speednum speed
   //Plan:
   //   Step 1: Apply drive to both wheels
   //Notes:
   //   01/14/15 - added left/right speed offset for straightness compensation
   //   01/22/15 - added code to restrict fast/slow values
   //   01/24/15 - revised for continuous run - no timing
   //  01/26/15 - speednum modifications moved to UpdateWallFollowMotorSpeeds()

   //leftspeednum = (leftspeednum <= MOTOR_SPEED_FULL)? leftspeednum : MOTOR_SPEED_FULL ;
   //leftspeednum = (leftspeednum >= MOTOR_SPEED_LOW)? leftspeednum : MOTOR_SPEED_LOW ;

   //rightspeednum = (rightspeednum <= MOTOR_SPEED_FULL)? rightspeednum : MOTOR_SPEED_FULL ;
   //rightspeednum = (rightspeednum >= MOTOR_SPEED_LOW)? rightspeednum : MOTOR_SPEED_LOW ;
   
//Step 1: Apply drive to both wheels
   digitalWrite(LeftMotorEnablePin, HIGH);
   digitalWrite(RightMotorEnablePin, HIGH);

   //analogWrite(LeftMotorSpeedPin,leftfinalspdnum + LEFT_SPEED_COMP_VALUE);
   //analogWrite(RightMotorSpeedPin,rightfinalspdnum + RIGHT_SPEED_COMP_VALUE);
   analogWrite(LeftMotorSpeedPin,leftspeednum + LEFT_SPEED_COMP_VALUE);
   analogWrite(RightMotorSpeedPin,rightspeednum + RIGHT_SPEED_COMP_VALUE);

////DEBUG!!
//   analogWrite(LeftMotorSpeedPin,MOTOR_SPEED_HALF + LEFT_SPEED_COMP_VALUE);
//   analogWrite(RightMotorSpeedPin,MOTOR_SPEED_HALF + RIGHT_SPEED_COMP_VALUE);
////DEBUG!!

   Serial.print("Run both Motors: l/r speeds are ");
   Serial.print(leftspeednum);
   Serial.print(", ");
   Serial.println(rightspeednum);
}

//12/26/14 added for independent motor speed
//01/13/15 rev to use time im Msec vs sec
//01/24/15 name chgd to RunBothMotorsMsec()
void RunBothMotorsMsec(int timeMsec,int leftspeednum, int rightspeednum)
{
   //Purpose: Run both motors for timesec seconds at speednum speed
   //Inputs:
   //   timesec = time in seconds to run the motors
   //   speednum = speed value (0 = OFF, 255 = full speed)
   //Outputs: Both motors run for timesec seconds at speednum speed
   //Plan:
   //   Step 1: Apply drive to both wheels
   //   Step 2: Delay timsec seconds
   //   Step 3: Remove drive from both wheels.
   //Notes:
   //   01/14/15 - added left/right speed offset for straightness compensation
   //   01/22/15 - added code to restrict fast/slow values

   int leftfinalspdnum = leftspeednum + LEFT_SPEED_COMP_VALUE;
   leftfinalspdnum = (leftfinalspdnum <= MOTOR_SPEED_FULL)? leftfinalspdnum : MOTOR_SPEED_FULL ;
   leftfinalspdnum = (leftfinalspdnum >= MOTOR_SPEED_LOW)? leftfinalspdnum : MOTOR_SPEED_LOW ;

   int rightfinalspdnum = rightspeednum + RIGHT_SPEED_COMP_VALUE;
   rightfinalspdnum = (rightfinalspdnum <= MOTOR_SPEED_FULL)? rightfinalspdnum : MOTOR_SPEED_FULL ;
   rightfinalspdnum = (rightfinalspdnum >= MOTOR_SPEED_LOW)? rightfinalspdnum : MOTOR_SPEED_LOW ;
   
//Step 1: Apply drive to both wheels
   digitalWrite(LeftMotorEnablePin, HIGH);
   digitalWrite(RightMotorEnablePin, HIGH);

   analogWrite(LeftMotorSpeedPin,leftfinalspdnum);
   analogWrite(RightMotorSpeedPin,rightfinalspdnum);

   Serial.print("Run both Motors: l/r speeds are ");
   Serial.print(leftfinalspdnum);
   Serial.print(", ");
   Serial.println(rightfinalspdnum);
}

void StoreDistInfo()
{
   //Nothing here yet
    //Serial.println("In StoreDistInfo()");

////DEBUG!!
//   getFreeRam();
////DEBUG!!

}



bool RotateCWDeg(bool bCW,int numdeg)
{
   //Purpose: Rotate the robot CW or CCW numdeg degrees
   //Inputs:
   //   bCW = true for CW rotation, false for CCW
   //   numdeg = integer denoting number of degrees to rotate
   //Outputs: robot rotated CW or CCW by numdeg degrees
   //Plan:
   //   Step 1: Set wheel directions appropriately
   //   Step 2: Drive both wheels long enough to accomplish numdeg degrees of rotation
   //   Notes:
   //   01/13/15 rev to calc msec vs sec

//DEBUG!!
   Serial.print("In RotateCWDeg: bCW = ");
   Serial.print(bCW);
   Serial.print(", numdeg = ");
   Serial.println(numdeg);
//DEBUG!!

//Step 1: Set wheel directions appropriately
   if(bCW)
   {
      //left wheel forward, right wheel reverse
      SetLeftMotorFwdDir(true);
      SetRightMotorFwdDir(false);
   }
   else //must be CCW
   {
      //left wheel reverse, right wheel forward
      SetLeftMotorFwdDir(false);
      SetRightMotorFwdDir(true);
   }

//Step 2: Drive both wheels long enough to accomplish numdeg degrees of rotation
   double drivetimeMsec = 1000 * 0.5 * (numdeg/DriveWheelDegPerSec); //0.5 because both wheels are turning

//DEBUG!!
   Serial.print("drivetimeMsec = ");
   Serial.println(drivetimeMsec);
//DEBUG!!

   //01/24/15 now have separate functions for continuous vs timed runs
   //RunBothMotors(drivetimeMsec,MOTOR_SPEED_HALF,MOTOR_SPEED_HALF);
   RunBothMotorsMsec(drivetimeMsec,MOTOR_SPEED_HALF,MOTOR_SPEED_HALF);
      
   return true;
}

void UpdateWallFollowMotorSpeeds(int &leftspeednum, int &rightspeednum)
{
   //Purpose:  Update left & right motor speed values to follow the nearest wall
   //Provenance: Created 12/26/14
   //Inputs:
   //   leftspeednum = integer denoting left motor drive value (0-255)
   //   rightspeednum = integer denoting right motor drive value (0-255)
   //   previous distance input from left & right ping sensors
   //   current distance input from left & right ping sensors
   //Outputs:
   //   leftspeednum = integer denoting left motor drive value (0-255)
   //   rightspeednum = integer denoting right motor drive value (0-255)
   //Plan:
   //   Step1: Determine if we are closer to left or right walls & adjust speed accordingly
   //Notes:
   //   01/22/15 added some LED management code for debugging purposes
   //   01/25/15 rev to use TweakVal * |d1-d0| instead of just TweakVal

   int prevrightspdnum, prevleftspdnum; //added 01/22/15 for LED mgmt
   prevrightspdnum = rightspeednum;
   prevleftspdnum = leftspeednum;

//Step1: Determine if we are closer to left or right walls & adjust speed accordingly
   //01/21/15 now using NewPing class
   //01/26/15 rev to use median distance function
   int leftdistval = LeftPing.ping_cm();
   int rightdistval = RightPing.ping_cm();
   //int leftdistval = GetLeftMedianDistCm();
   //int rightdistval = GetRightMedianDistCm();

   //01/21/15 bugfix: don't adjust at all if both distances are == 0
   if( leftdistval * rightdistval == 0)
   {
      return;
   }

   //01/24/15 rev to use abs val vs pct for tweak
   int leftspdadjval = DriveWheelWallFollowTweakVal;
   int rightspdadjval = DriveWheelWallFollowTweakVal;

   ////01/25/15 added for slope-sensitive tracking adj. Can't do subtraction inside the abs().
   //int left_delta_dist = leftdistval-prevleftdistval;
   //left_delta_dist = abs(left_delta_dist);
   //int right_delta_dist = rightdistval-prevrightdistval;
   //right_delta_dist = abs(right_delta_dist);

   if(leftdistval <= rightdistval) //tracking wall on left side
   {
      Serial.println("tracking left wall");
      if (leftdistval > prevleftdistval)
      {
         Serial.println("going away from left wall");
         //going away from wall - slow left motor, speedup right motor
         leftspeednum -= leftspdadjval;
         rightspeednum += rightspdadjval;
         //leftspeednum -= (leftspdadjval * left_delta_dist);
         //rightspeednum += (rightspdadjval * right_delta_dist);

         //01/24/15 signal for left turn
         LeftTurnSignal();
      }
      //else
      else if(leftdistval < prevleftdistval)
      {
         Serial.println("going toward left wall");
         //going toward wall - slow right motor, speedup left motor
         leftspeednum += leftspdadjval;
         rightspeednum -= rightspdadjval;
         //leftspeednum += (leftspdadjval * left_delta_dist);
         //rightspeednum -= (rightspdadjval * right_delta_dist);

         //01/24/15 signal for right turn
         RightTurnSignal();
      }
      else
      {
         //equal case bypassed for no change
         Serial.println("no change");
         AllLedsOff();
      }
   }
   else //right wall is closer
   {
      Serial.println("tracking right wall");
      if (rightdistval > prevrightdistval)
      {
         //going away from wall - speedup left motor, slow right motor
         Serial.println("going away from right wall");
         leftspeednum += leftspdadjval;
         rightspeednum -= rightspdadjval;
         //leftspeednum += (leftspdadjval * left_delta_dist);
         //rightspeednum -= (rightspdadjval * right_delta_dist);

         //01/24/15 signal for right turn
         RightTurnSignal();
      }
      //else
      else if(rightdistval < prevrightdistval)
      {
         //going toward wall - speedup right motor, slow left motor
         Serial.println("going toward right wall");
         leftspeednum -= leftspdadjval;
         rightspeednum += rightspdadjval;
         //leftspeednum -= (leftspdadjval * left_delta_dist);
         //rightspeednum += (rightspdadjval * right_delta_dist);

         //01/24/15 signal for left turn
         LeftTurnSignal();
      }
      else
      {
         //equal case bypassed for no change
         Serial.println("no change");
         AllLedsOff();
      }
   }

   prevleftdistval = leftdistval;
   prevrightdistval = rightdistval;

////DEBUG!!
//   Serial.print("Left/Right dist & speed values: ");
//   Serial.print(leftdistval);
//   Serial.print(", ");
//   Serial.print(rightdistval);
//   Serial.print(" CM, ");
//   Serial.print(leftspeednum);
//   Serial.print(", ");
//   Serial.print(rightspeednum);
//   Serial.println(" spd units, ");
////DEBUG!!

   //01/24/15 removed speed limiting code - done in RunBothMotors()
   //01/26/15 put back - this is the only function that should modify speednums
   leftspeednum = (leftspeednum <= MOTOR_SPEED_FULL)? leftspeednum : MOTOR_SPEED_FULL ;
   leftspeednum = (leftspeednum >= MOTOR_SPEED_LOW)? leftspeednum : MOTOR_SPEED_LOW ;

   rightspeednum = (rightspeednum <= MOTOR_SPEED_FULL)? rightspeednum : MOTOR_SPEED_FULL ;
   rightspeednum = (rightspeednum >= MOTOR_SPEED_LOW)? rightspeednum : MOTOR_SPEED_LOW ;
}

void LeftTurnSignal()
{
   //Purpose:  Turns left Green LED on, rest off (LOW is ON)
   //Provenance: Created 01/24/15 gfp

   AllLedsOff();
   digitalWrite(leftGRNpin, LOW);
}

void RightTurnSignal()
{
   //Purpose:  Turns right Green LED on, rest off (LOW is ON)
   //Provenance: Created 01/24/15 gfp

   AllLedsOff();
   digitalWrite(rightGRNpin, LOW);
}

void StopSignal()
{
   //Purpose:  Turns left/right Red LEDs on, both Green LED's off (LOW is ON)
   //Provenance: Created 01/24/15 gfp

   digitalWrite(leftGRNpin, HIGH);
   digitalWrite(rightGRNpin, HIGH);
   digitalWrite(rightREDpin, LOW);
   digitalWrite(leftREDpin, LOW);
}

void BackupSignal()
{
   //Purpose:  Turns all 4 LEDs on (LOW is ON)
   //Provenance: Created 01/24/15 gfp

   digitalWrite(leftGRNpin, LOW);
   digitalWrite(rightGRNpin, LOW);
   digitalWrite(rightREDpin, LOW);
   digitalWrite(leftREDpin, LOW);
}


void AllLedsOff()
{
   //Purpose:  Turns all 4 LEDs off (LOW is ON)
   //Provenance: Created 01/24/15 gfp

   digitalWrite(leftGRNpin, HIGH);
   digitalWrite(rightGRNpin, HIGH);
   digitalWrite(rightREDpin, HIGH);
   digitalWrite(leftREDpin, HIGH);
}


//01/26/15 added to use NewPing's median distance function
int GetRightMedianDistCm()
{
   int timeUsec = RightPing.ping_median();
   int distCm = RightPing.convert_cm(timeUsec);
   return distCm;
}

//01/26/15 added to use NewPing's median distance function
int GetLeftMedianDistCm()
{
   int timeUsec = LeftPing.ping_median();
   int distCm = LeftPing.convert_cm(timeUsec);
   return distCm;
}

Simulator_admin
Site Admin
Posts: 237
Joined: Thu Feb 02, 2012 6:07 pm

Re: Variables aren't available to change until too late

Postby Simulator_admin » Mon Feb 09, 2015 10:15 am

Hi Frank, OK, thanks for the sketch. With breakpoints, when a breakpoint has interrupt the program, the current line is not executed. We just tried this on the sketch and this is correct.

With a line such as int i=foo(5), the subroutine foo(5) will first need to be run before the variable should be initialised. With the Simulator, it will branch to the subroutine, get the return value of foo(5) and then substitute this result into the line int i = foo(5);

Usually, a variable cannot be used before it is initialised. A work around for any issues would be to split this into two lines as suggested:

Code: Select all

int i
i = foo(5);


Hope this helps.

Kallapser
Posts: 2
Joined: Sun May 07, 2017 4:03 am

Variables arent available to change until too late

Postby Kallapser » Thu May 11, 2017 3:22 am

Hello Wilson,

No I did not touch the firewall, I would not know how.

I installed r849 and I still get the same error.

What can I do now ?

Thank you
Pierre





Return to “Version 0.99”

Who is online

Users browsing this forum: No registered users and 1 guest