View unanswered posts | View active topics It is currently Sat Aug 18, 2018 4:07 pm

 Page 1 of 1 [ 7 posts ]
 Print view Previous topic | Next topic
Force Sensor Update? Is my code crippled?
Author Message
Rookie

Joined: Wed Mar 07, 2007 1:28 pm
Posts: 4
Location: Zürich Switzerland
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

Thu May 03, 2007 12:28 pm
Creator

Joined: Fri Feb 09, 2007 9:21 am
Posts: 616

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  }}

Fri May 04, 2007 2:56 am
Novice

Joined: Sun Feb 04, 2007 12:48 am
Posts: 69
Location: Australia

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

Fri May 04, 2007 3:02 am
Creator

Joined: Fri Feb 09, 2007 9:21 am
Posts: 616

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.

Fri May 04, 2007 3:21 am
Rookie

Joined: Wed Mar 07, 2007 1:28 pm
Posts: 4
Location: Zürich Switzerland

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

Fri May 04, 2007 5:45 am

Joined: Wed Jan 24, 2007 10:44 am
Posts: 442
Location: Pittsburgh, PA

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).

_________________
Vu Nguyen
Software Training Development Team | Webmaster
Need more support? Email ROBOTC Support at support@robotc.net to put in a support ticket

Robotc.net| Robomatter Store | Robotics Academy | CS2N

Fri May 04, 2007 8:31 am
Creator

Joined: Fri Feb 09, 2007 9:21 am
Posts: 616

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.

Fri May 04, 2007 8:32 am
Display posts from previous:  Sort by
 Page 1 of 1 [ 7 posts ]

#### Who is online

Users browsing this forum: No registered users and 2 guests

 You cannot post new topics in this forumYou cannot reply to topics in this forumYou cannot edit your posts in this forumYou cannot delete your posts in this forumYou cannot post attachments in this forum

Search for:
 Jump to:  Select a forum ------------------ ROBOTC Applications    ROBOTC for LEGO MINDSTORMS       Third-party sensors    ROBOTC for CORTEX & PIC    ROBOTC for VEX IQ    ROBOTC for Arduino    Robot Virtual Worlds    Multi-Robot Communications    Issues and Bugs Competitions & Partners    Mini Urban Challenge    CS2N Robot Virtual Worlds Competitions       VEX Skyrise Competition 2014-2015       VEX Toss Up 2013-2014       FTC Block Party! 2013-2014    Competitions using VEX - BEST, TSA, VEX, and RoboFest!    FTC Programming    RoboCup Junior and Other ROBOT Competitions Virtual Brick Robotics Discussions    General Discussions    Project Discussions Off-Topic ROBOTC Forum & ROBOTC.net Suggestions/Feedback    ROBOTC Forums Suggestions/Comments    ROBOTC.net Suggestions/Comments       NXT Programming: Tips for Beginning with ROBOTC       VEX Programming: Tips for Beginning with ROBOTC    2013 Robotics Summer Of Learning       VEX Toss Up Programming Challenge       FTC Ring It Up! Programming Challenge    International Forums       Spanish Forums          ROBOTC for MINDSTORMS          ROBOTC for VEX       French Forums          ROBOTC pour Mindstorms          ROBOTC pour IFI VEX       Japanese Forums （日本語のフォーラム）       German Forums    2015 Spring Carnival Event    PLTW (Project Lead The Way)    Robotics Merit Badge    2014 Robotics Academy Summer of Learning