ROBOTC.net forumshttp://www.robotc.net/forums/ Force Sensor Update? Is my code crippled?http://www.robotc.net/forums/viewtopic.php?f=1&t=153 Page 1 of 1

Author:  CranoBot [ Thu May 03, 2007 12:28 pm ]
Post subject:  Force Sensor Update? Is my code crippled?

Hi

I am quite new to RobotC and C++ in general. After some training lessons I came up with this program:

 Code:const tSensors touch                = (tSensors) S1;   //sensorTouch        //*!!!!*//const tSensors sonar                = (tSensors) S4;   //sensorSONAR9V      //*!!!!*//task main(){  const TSynchedMotors ourMotorSyncSetting = synchCB;  nSyncedMotors = ourMotorSyncSetting;  nSyncedTurnRatio = 100;   while (true){      if(SensorValue(touch) == 0 && SensorValue(sonar) > 30){        nSyncedTurnRatio = 100;        motor[motorC] = -38;      }      else{          nSyncedTurnRatio = 100;          motor[motorC] = 27;          wait10Msec (85);          nSyncedTurnRatio = -28;          motor [motorC] = -24;          wait10Msec (70);          while (SensorValue(sonar) < 36){           nSyncedTurnRatio = -28;             motor [motorC] = -24;            wait10Msec(10);            PlayTone(SensorValue(sonar) * 100 , 5);            if ( !(SensorValue(touch) == 0)){               nSyncedTurnRatio = 100;                motor[motorC] = 27;                wait10Msec (85);            }       }      }   }}

Note that "-" for motors means forward.

The Robot is supposed to go straight ( which it still doesn't although the motors b and c are synched. The motors do not turn with the same speed) until it hits a wall either with sonic or touch. It reverses and turns. If the sonic value is still too low it keeps turning. If touch is 1 wile turning it reverses again until its free. During this period a beeper is implemented so I can hear what the sensor value is. the problem is that the robot keeps turning until very long after no wall is in range anymore. The beeper showed me that the sensor value is still recognized as low. Is there a way to force a sensor update in that loop? Whats the problem in general. Seriously guys i have no experience so any help and comment is welcome.

THX

CranoBot

Author:  Dick Swan [ Fri May 04, 2007 2:56 am ]
Post subject:

Try the following code.
It's untested so if may have some problems. Play with the RobotC Debugger -- single step mode -- to follow your program execution and see if the logic is working.

 Code:const tSensors touch                = (tSensors) S1;   //sensorTouch        //*!!!!*// const tSensors sonar                = (tSensors) S4;   //sensorSONAR9V      //*!!!!*// task main(){   const TSynchedMotors ourMotorSyncSetting = synchCB;   while (true)  {     //    // Go straight until obstacle    //    nSyncedMotors = ourMotorSyncSetting;     nSyncedTurnRatio = 100;     motor[motorC] = -38;     while ((SensorValue(touch) == 0) && (SensorValue(sonar) > 30)    {       // Keep moving straight. No obstacle    }    //    // Obstacle detected. Backup and turn until no more obstacle.    //    while (true)    {      // Backup            nSyncedTurnRatio = 100;       motor[motorC] = 27;       wait10Msec (85);             // Turn      nSyncedTurnRatio = -28;       motor [motorC] = -24;       wait10Msec(10);       if ((SensorValue(touch) == 0) && (SensorValue(sonar) > 30)        break; // Obstacle is cleare    }        // No more obstacle. We can loop again, going straight  }}

 Author: JamesD [ Fri May 04, 2007 3:02 am ] Post subject: I don't have time to decode your work now but if you're anything like me you'd prefer a quick half response to no response for a while. One issue you might be having is that SensorValue(sonar) can exhibit a behaviour where the lowest value "sticks" and doesn't change until a lower value is read and "sticks" again. This reduces the likelihood of accidental misreads (which the sonar can easily do) mucking up your program. If you want the reading to be more dynamic use SensorRaw(sonar) it will have a much greater range ie 0-1000 Hope this helps James

 Author: Dick Swan [ Fri May 04, 2007 3:21 am ] Post subject: James: YOu're right. The "SensorRaw" will not mask invalid values. But the range is still 0-255 and not 0-1023. The values all fit in a single byte for the sonar / ultransonic sensor. Other sensors do use the larger range.

Author:  CranoBot [ Fri May 04, 2007 5:45 am ]
Post subject:

Dick Swan wrote:
Try the following code.
It's untested so if may have some problems. Play with the RobotC Debugger -- single step mode -- to follow your program execution and see if the logic is working.

 Code:const tSensors touch                = (tSensors) S1;   //sensorTouch        //*!!!!*// const tSensors sonar                = (tSensors) S4;   //sensorSONAR9V      //*!!!!*// task main(){   const TSynchedMotors ourMotorSyncSetting = synchCB;   while (true)  {     //    // Go straight until obstacle    //    nSyncedMotors = ourMotorSyncSetting;     nSyncedTurnRatio = 100;     motor[motorC] = -38;     while ((SensorValue(touch) == 0) && (SensorValue(sonar) > 30)    {       // Keep moving straight. No obstacle    }    //    // Obstacle detected. Backup and turn until no more obstacle.    //    while (true)    {      // Backup            nSyncedTurnRatio = 100;       motor[motorC] = 27;       wait10Msec (85);             // Turn      nSyncedTurnRatio = -28;       motor [motorC] = -24;       wait10Msec(10);       if ((SensorValue(touch) == 0) && (SensorValue(sonar) > 30)        break; // Obstacle is cleare    }        // No more obstacle. We can loop again, going straight  }}

Thanks for your answers so far. I will try your code too as well as the raw sensor. But will it not keep hanging in the second while (true) loop? Or does the break; command let the program jump out of the loop. Maybe I have overseen something but does anyone know a link to a table of all possible commands and operations in RobotC?

THX again

CranoBot

 Author: vnguyen [ Fri May 04, 2007 8:31 am ] Post subject: You're right, it will exit the second while(true) with the break; command. And it will run that command when that condition is fulfilled (Touch sensor is pushed and the sonar sensor is more than 30 cm away).

 Author: Dick Swan [ Fri May 04, 2007 8:32 am ] Post subject: the "break;" command is a standard 'C' construct to exit a loop. And also txit a 'switch' block. It's one of three useful "control flow of program execution" statements. "break" is one; "return" -- exit a function -- is another. The third is "continue" -- which means end the current loop iteration and start the next iteration of the loop. In a "for" loop, this will move to the next iteration value. In a "while" loop it will move to the "test loop condition" value check. You need to understand basic 'C' syntax to work with RobotC. Most of the additinoal RobotC internal functions are given in the left hand window pane in the RobotC IDE.

 Page 1 of 1 All times are UTC - 5 hours [ DST ] Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Grouphttp://www.phpbb.com/