View unanswered posts | View active topics It is currently Wed Oct 01, 2014 12:01 am






Reply to topic  [ 7 posts ] 
Force Sensor Update? Is my code crippled? 
Author Message
Rookie

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

Joined: Fri Feb 09, 2007 9:21 am
Posts: 615
Post 
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
Profile
Novice

Joined: Sun Feb 04, 2007 12:48 am
Posts: 69
Location: Australia
Post 
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
Profile
Creator
Creator

Joined: Fri Feb 09, 2007 9:21 am
Posts: 615
Post 
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
Profile
Rookie

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

Joined: Wed Jan 24, 2007 10:44 am
Posts: 439
Location: Pittsburgh, PA
Post 
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? Use the ROBOTC Ticketing system

Robotc.net| Robomatter Store | Robotics Academy | CS2N


Fri May 04, 2007 8:31 am
Profile
Creator
Creator

Joined: Fri Feb 09, 2007 9:21 am
Posts: 615
Post 
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
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 7 posts ] 

Who is online

Users browsing this forum: No registered users and 2 guests


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

Search for:
Jump to:  



Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software for PTF.