Coding Encoders and Ultrasonic at the same time HELP!!!

Hey Guys,
I am trying to program my robot to move the motors according to encoders. I also hooked up a ultrasonic sensor at the front. Basically my idea is that during autonomous, if we can make the motors move with the encoders like usual but also stop if there is something in front of it that is in the way.
I have only been programming for about 3 months. Please help. I am freaking out before a competition.
Thanks!

ilovecode

The ultrasonic sensor returns a value that is equal to the distance away from any object in inches (I think).

To get the robot to stop you have to define a threshold (int threshold) then check every 25 or so milliseconds whether the distance away from the object is smaller than the threshold (SensorValue[ultrasonic] < threshold) and if it is then tell all the drive motors to stop then possibly run another action.

This is the simplest thing you could do, you may want to search the forums for PID if you want to get a little more involved, though for a simple robot the above code will work fine.

If you are using robotC you can select in motor/sensor config what unit the ultrasonic returns you can use raw, inches, cm, and, mm
my code would look like this:


motor+sensor setup stuff

int threshold = 10;// how many units away you will stop at
void auto(int ticks){
   sensorValue[encoder] = 0;//clear encoder 
   while(ticks > SensorValue[encoder] && SensorValue[ultrasonic] < threshold){
      motor[leftDrive] = 127;
      motor[rightDrive] = 127;
   }
   motor[leftDrive] = 0;
   motor[rightDrive] = 0;
}
task autonomous(){
auto(100);//go 100 ticks
}

The utlrasonics are a bit finicky too. Many times you may get a -1 from the ultrasonic as it got nothing for a period of time as the wave bounced away instead of right back at it.

So you may want to make a function around detecting a blockage. Just doing this off the top of my head. You want to detect if the ultrasonic sensor is giving you a good value so handling it’s data in a function makes some sense so you don’t repeat that same error handling code over and over again.

Play with the ultrasonic in the debugger and see all the crazy values you get wile moving it about your course. (manually push the robot around is the best bet and move things in front of the robot while viewing the sensors tab in the debugger)


 int threshold = 10;

int detectBlockage(int my_sensor_port)
{
   
   if(SensorValue[my_sensor_port] == -1)
  { 
     // nothing in the way since it returns nothing (or you are waaay too close) 
     // you could change this if you wanted to a new return value
     return 0;
  }
  else if(SensorValue[my_sensor_port] > threshold)
{ 
    // we will say 1 is something in the way
     return 1;
  }
  else
  { 
      return 0;
  }
}


main()
{

  sensorValue[encoder] = 0;//clear encoder 
   while(ticks > SensorValue[encoder] && detectBlockage(ultrasonic) !=1){
      motor[leftDrive] = 127;
      motor[rightDrive] = 127;
   }
   motor[leftDrive] = 0;
   motor[rightDrive] = 0;
}