Robot code & FLOW CHART

Flowchart

We decided to have an overarching while loop for the touch sensor so that the robot would be in constant motion, with all other while loops and functions would be situated after it in order to maintain a hierarchy of actions. Firstly, if the touch sensor was not activated, the robot would continuously move forward unless it reached its end goal. However, if the touch sensor was activated, the robot would back up and begin to turn right. It would then scan the area in front of it, looking for obstructions. If the robot sensed any obstacle, it would then turn right again until it made a complete 180 degree turn and return to the touch sensor function to move forward. If it did not sense any obstacle, it would immediately return to the touch sensor function without making a 180 degree turn. All in all, we wanted our robot to detect obstacles and move around them while also pursuing its destination.

First Version of Code

#pragma config(StandardModel, "RVW Buggybot")

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


task main()

{

while(true)

{

while(SensorValue(touchSensor)== 0)

{

motor[rightMotor] = -127;

motor[leftMotor] = 127;

}

while(SensorValue(touchSensor)==1)

{

motor[rightMotor] = 0;

motor[leftMotor] = 0;

wait1Msec(500);

motor[rightMotor]= 127;

motor[leftMotor]= -127;

wait1Msec(600);


motor[rightMotor] = 0;

motor[leftMotor] = 127;

wait1Msec(485);

}


if(SensorValue(sonarSensor)< 50) {

motor[rightMotor] = 0;

motor[leftMotor] = 127;

wait1Msec(770);

}

}

}


This is the first version of code that was able to successfully make it through the course; however, it wouldn't always complete the course. It would reach the end around 40% to half of the time. The two main problems that contributed to this were the imprecise turning and the sonar sensor value. The robot's turning would vary most test runs even though the code was exactly same, causing the robot to crash into walls and flip during the course as it would never travel in straight lines. The sonar sensor value was a bit too high, since it would sense objects that weren't obstacles and still try to move around them.

Second Version of Code

#pragma config(StandardModel, "RVW Buggybot")

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


task main()

{

while(true)

{

while(SensorValue(touchSensor)== 0)

{

motor[rightMotor] = -127;

motor[leftMotor] = 127;

}

while(SensorValue(touchSensor)==1)

{


motor[rightMotor]= 127;

motor[leftMotor]= -127;

wait1Msec(500);

motor[rightMotor] = 0;

motor[leftMotor] = 0;

wait1Msec(200);

motor[rightMotor] = 127;

motor[leftMotor] = 127;

wait1Msec(250);


}

if(SensorValue(sonarSensor)< 35) {

motor[rightMotor] = 127;

motor[leftMotor] = 127;

wait1Msec(450);

}

}

}


The second version of code was just refining the movements of the robot. Although the imprecise turning could never be fully fixed, it could be minimized. So, we decided to make the robot rotate instead of turn as turning caused more misalignment. Additionally, the sonar values were decreased so that the robot would only see objects in its immediate vicinity.