So, I’ve been attempting to code my weird robot in anticipation for a competition coming up, and I’ve completely hit a brick wall, after exhausting nearly all my options I turn to the world wide web.
So, a little thingy to note, my robot drives like a car, front wheel drive, and has a motor on top, that turns a separate plate underneath that allows it to turn. It’s not tank drive.
Second, I’ve been messing around with sensors to try and make it autonomous, and I pretty much just want it to stay within two line boundaries on either side of the robot, and to stop when an obstacle is close enough in front of it. The latter part of that was achieved with a sonar sensor, the robot drives forward till it’s 50cm to a wall or other obstacle, and then it stops. (I also coded it to move backwards if it’s too close but that part doesn’t really matter.)
The issue is with the line sensors, regardless of what I code I cannot even get them to respond, let alone turn the top motor. (Making the robot turn and avoid the lines on the edge, staying within the boundary).
If anyone could sift through my code and tell me what I’ve done wrong, it would be a great service, I have gone cross-eyed and spent around 10+ hours to no avail. Also I’m new to coding in general, been at it for maybe 3 weeks now, so any advice or more efficient methods would be good
#pragma config(Sensor, in1, leftline, sensorLineFollower)
#pragma config(Sensor, in2, rightline, sensorLineFollower)
#pragma config(Sensor, dgtl8, sonar, sensorSONAR_cm)
#pragma config(Motor, port1, rightmotor2, tmotorVex393_HBridge, openLoop, reversed)
#pragma config(Motor, port3, leftmotor1, tmotorVex393_MC29, openLoop, reversed)
//*!!Code automatically generated by 'ROBOTC' configuration wizard !!*//
int sonar_value;
int leftline_value;
int rightline_value;
int threshold = 45;
int distance = 50;
void driveforward()
{
motor[port1] = 60;
}
void drivereverse()
{
motor[port1] = -60;
}
void brake()
{
motor[port1] = (sonar_value - distance)*2;
}
void turnleft()
{
motor[port3] = 60;
}
void turnright()
{
motor[port3] = -60;
}
task main ()
{
while(1==1)
{
sonar_value = SensorValue[sonar];
leftline_value = SensorValue[leftline];
rightline_value = SensorValue[rightline];
if(sonar_value > 0)
{
driveforward();
}
else
{
brake();
}
if(sonar_value < 0)
{
drivereverse();
}
else
{
brake();
}
if(leftline_value > threshold)
{
turnleft();
wait1Msec(250);
}
else
{
driveforward();
}
if(rightline_value > threshold)
{
turnright();
wait1Msec(250);
}
}
}