View unanswered posts | View active topics It is currently Mon Dec 22, 2014 6:46 pm






Reply to topic  [ 2 posts ] 
Ultrasonic Reading of -1? 
Author Message
Rookie

Joined: Fri Jul 26, 2013 4:29 pm
Posts: 5
Post Ultrasonic Reading of -1?
Hi,
While doing trying to run my robot for Sentry Simulation II, after clearing the first obstacle, the ultrasonic sensor will read -1 and stop the robot(as it runs in while loop whenever the reading is above 25). The ultrasonic sensor, I think, shouldn't even be able to read -1, only 0-255. So I think this might be a bug? And just in case, I attached my code.

-Thanks


Code:
#pragma config(StandardModel, "Virtual Worlds NXT")
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//

/* Virtual Worlds Configuration (Type - Name):
Motor Port A: NXT Motor - gripperMotor
Motor Port B: NXT Motor - rightMotor
Motor Port C: NXT Motor - leftMotor
Sensor Port 1: Touch Sensor - touch
Sensor Port 2: Compass Sensor - compass
Sensor Port 3: Light Sensor - light
Sensor Port 4: Sonar Sensor - sonar
*/

void forward(int p, int t){
   motor[motorC] = p;
   motor[motorB] = p;
   wait1Msec(t);
}

void leftPoint(int t){
   motor[motorC] = -20;
   motor[motorB] = 20;
   wait1Msec(t);
}

void rightPoint(int t){
   motor[motorC] = 20;
   motor[motorB] = -20;
   wait1Msec(t);
}

void stop(int t){
   motor[motorC] = 0;
   motor[motorB] = 0;
   wait1Msec(t);
}

//gives power and number of encoder counts for robot to move forward
void forwardEncoded(int p, int e){
   nMotorEncoder[leftMotor] = 0;
   while(nMotorEncoder[leftMotor] < e){
      motor[leftMotor] = p;
      motor[rightMotor] = p;
   }
}

//give a negative encoder value and negative power value
void backwardEncoded(int p, int e){
   nMotorEncoder[leftMotor] = 0;
   while(nMotorEncoder[leftMotor] > e){
      motor[leftMotor] = p;
      motor[rightMotor] = p;
   }
}

void rightPointEncoded(int p, int e){
   nMotorEncoder[leftMotor] = 0;
   while(nMotorEncoder[leftMotor] < e){
      motor[leftMotor] = p;
      motor[rightMotor] = -p;
   }
}

void leftPointEncoded(int p, int e){
   nMotorEncoder[rightMotor] = 0;
   while(nMotorEncoder[rightMotor] < e){
      motor[leftMotor] = -p;
      motor[rightMotor] = p;
   }
}
//note time passed is in 10 ms intervals
void encoderLineFollow(int e){
   nMotorEncoder[rightMotor] = 0;
   nMotorEncoder[leftMotor] = 0;

   while(nMotorEncoder[leftMotor] < e && nMotorEncoder[rightMotor] < e){
      //line follow
      if(SensorValue[light] < 43) {
      motor[leftMotor]  = 5;
      motor[rightMotor] = 35;
      }
      else{
      motor[leftMotor]  = 35;
      motor[rightMotor] = 5;
      }

   }
}
   //aligns to the given compass sensor reading
   //note: doesnt work when reading has to wrap around 360 ie: current reading is 259, given parameter is 1
void align(int direction){
   while (SensorValue[compass] < direction){
      motor[leftMotor] = 10;
   }
   while(SensorValue[compass] > direction){
      motor[rightMotor] = 10;
   }
}

task main()
{
   while(true){
      
      while(SensorValue[sonar] > 25){
         
         if(SensorValue[light] < 50){
            rightPointEncoded(20,300);
            stop(500);
         }
         
         else{
            motor[leftMotor] = 75;
            motor[rightMotor] = 75;
         }
         
      }
      
      motor[leftMotor] = 0;
      motor[rightMotor] =0 ;
   }
   
}


Edit: Used google, and found another poston this forum on the same problem that said a reading of -1, indicated nothing to detect, but the RobotC wiki here:http://www.robotc.net/wiki/NXT_Sensors_Overview
states that a reading of 255 indicates out of range/no detectable objects. Do the sensors function differently in physical form compared to RVW? Or am I just understanding it wrong :p?


Sun Jul 28, 2013 12:42 pm
Profile
Site Admin
Site Admin
User avatar

Joined: Tue Oct 09, 2012 10:34 am
Posts: 192
Post Re: Ultrasonic Reading of -1?
Hi sifu,
I believe the sonar sensor code was originally written based off of the VEX Sonar Sensor, which returns -1 when no object is detected. I believe we've left it that way for NXT, despite being not quite the same behavior as on the physical hardware, because it makes it easier to distinguish between the "no object" and "object very far away" cases. Due to technical reasons (I've included an explanation below, if you're curious), there's a fairly simple fix for this: simply prepend (ubyte) onto the front of your SensorValue expression, like this:

Code:
(ubyte)SensorValue(sonarSensor)

This shouldn't change the behavior of the code on the physical hardware, but will make the virtual sensor readings match the physical readings

Cheers,
--Ryan


    How -1 Becomes 255:
    Computers represent negative numbers using something called Two's Complement Notation. There's plenty of good articles and YouTube videos on the subject (as well as binary numbers, if you aren't familiar with them), so I won't explain it here, but I encourage you to look into it more if you're curious. The critical part of this is you can get the Two's Complement Notation of a negative number by writing out the binary for the positive version of the number, then flip all the bits (change 0s to 1s and vice versa), then add 1.

    The other necessary piece of information is that NXTs use 8-bit numbers to represent sensor values.

    Let's try writing the 8-bit, Two's Complement Notation for -1:

    We write the positive version in binary: 00000001
    We flip all the bits: 11111110 - which is 254 in decimal
    Then we add 1: 255 - or 11111111 in binary

    So using 8-bit numbers, -1 and 255 have the same representation in memory.

    So the only real difference between the VEX values and NXT values is that NXT uses 8-bit numbers and VEX uses 16-bit numbers, so VEX has enough bits that it's able to distinguish between -1 and 255. Putting the (ubyte) in front of the sensor value forces ROBOTC to treat the sensor value as an 8-bit number.

_________________
Ryan Cahoon
CMU Robotics Academy
RVW Software Developer

Robot Potato Head; Virtual NXT


Sun Jul 28, 2013 6:27 pm
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 2 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.