Thursday, May 13, 2010

An Intro




I recently had the urge to revisit the Vex robotics kit I purchased on a visit to the States back in 2006. I had never done more than build a basic tank treaded vehicle that my grandson could drive around with the remote control so I was keen to try out the Easy C program that came with it.


The short of it was I had waited too long to register the program and I couldn't even use it so I found the RobotC program after a bit of looking on the web. I am still using the 30 day trial but it seems quite good.

I built the tank tread platform above as a simple experimental base (and it also neatly mounts my netbook but that is a separate story). The platform has two independent motors powering the tank treads and an ultrasonic sensor in the front mounted on a Vex servo.

I found sample code a little hard to come by (except for the excellent printable lessons on the robotc.net website) so I decided to share mine. I have included the coding for a short program that allows the vexbot to wander around the room avoiding walls AND chair and table legs (that last bit is a whole lot trickier).



code begins here

#pragma config(Sensor, in5, Ultrasonic, sensorSONAR, int1)

//*!!Code automatically generated by 'ROBOTC' configuration wizard !!*//

/*************************************************************************
VEX - Forward until Close Proximity

Description: This program is a modified version of the code found on the robotc.net website
instructs the robot to drive forward at half
speed until the ultrasonic sensor detects an obstruction within 15 inches
of it. There is a two second pause at the beginning of the program.

Configuration: This program is written to work with a modified version of
the Squarebot model.
Right Motor - port2
Left Motor - port3
servo - port6
Ultrasonic - input 5 + interrupt port 1

Additional Notes:
- The "bMotorReflected[port2] = 1;" is needed with the Squarebot model,
but may not be needed for all robot configurations.
- If the robot is so far away that the ultrasonic sensor detects nothing,
it will return a value of -1 . The range of the ultrasonic sensor is from
0 to 255 inches.
*************************************************************************/

void moveStraight()
{
int x=0;
//Loop while robot's Ultrasonic sensor is further than 15 inches away from an object
//note sensor returns -1 if it gets no reflection ie distance is too great so test for
//less that 0 as well when reading sensor
while (x <> 15 SensorValue(Ultrasonic) <0>
{
//do nothing }
else
{
//turnRight
motor[port2] = -63; //Motor on port2 is run at half (63) power forward
motor[port3] = 63; //Motor on port3 is run at half (63) power forward
wait1Msec(3000); //run for 3 seconds to perform a reasonable turn }

motor[port2] = 63; //Motor on port2 is run at half (63) power forward
motor[port3] = 63; //Motor on port3 is run at half (63) power forward
wait1Msec(250); //run for 250 milliseconds then check the sensor again
x=x+1; //do this 3 times then go back and move the sensor }
}

void rotateSensorRight()
{
motor[port6] = -90; }

void rotateSensorLeft()
{
motor[port6] = 90; }

void centerSensor()
{
motor[port6] = 0; }

task main()
{

wait1Msec(2000); //Robot waits for 2000 milliseconds before executing program
bMotorReflected[port2] = 1; //Reflects the direction of the motor on port2

while(true) //run forever
{
rotateSensorRight();
moveStraight();
centerSensor();
moveStraight();
rotateSensorLeft();
moveStraight();
centerSensor();
moveStraight(); }

}