Line Tracking issue . . . please help

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);
		}
	}
}

With the current code, does the robot still drive forwards and backwards with the sonar sensor’s input? The problem is just that the light sensors are not doing anything? And have you gotten the front top wheel to turn without any sensor input (just have it spin)?

Edit: It’s been a while since I’ve done RobotC so this may be how it should be, but your two motors are different types.

Also while it can be good for expansion later, making functions that only do one short line of code is redundant. It makes it harder to read and adds clutter when all your function does is call another function with no extra arguments. This is especially true when you only call it once in the program.

I would also work on the order you have your if statements at the end. If(), else if(), then else if you are applying to one set of motors. Don’t do if() else if() for one set. It can make it harder to read and also change how the logic works (bottom to top becomes top to bottom of what runs).

I would also rename your motors to something more clear like motorTurn and motorDrive instead of left and right motors. You should also be able to replace the “portX” with the motor name too so it is a bit clearer what you are changing.

You also have your names for variables with underscores. That’s fine. What is harder to read though are your function names. Those should be in camelCase, with an underscore, or something to indicate each word (otherwise it gets hard to read). So moveforward would be moveForward, MoveForward or move_forward.

Constants also normally are seperated as well. Normally by being UPPER_CASE. Constants are just variables that don’t change. You can specify its a constant using macros, or in C++ you can use the const keyword (might be for C too iirc).

1 Like