Questions about program...

Hey, my name is Marshall from team 11761B, and I am an all around person… but at the moment I am programming. My teammates are being to slow for me to test out my programs, and I am fairly new to robotC… so I was wondering if anybody could see any major errors besides the fact that none of the motors or sensors are assigned due to the fact that I don’t know what port they would be connected to yet. If anybody could help me out that would be great… thanks in advance!!!

P.S. The plan in the program was to make some functions so future programming would be easier!

#define abs(X) ((X < 0) ? -1 * X : X)

void driveForward(float inches, int power)
{
SensorValue[rightencoder] = 0;
SensorValue[leftencoder] = 0;

int drivegoal = (inches * 49.9109902106);

while(abs(SensorValue[rightencoder] < drivegoal || SensorValue[leftencoder] < drivegoal))
{
	motor[rightmotor] = power;
	motor[leftmotor] = power;
}

{
	motor[rightmotor] = 0;
	motor[leftmotor] = 0;
}

}

void driveBackward(float inches, int power) //New
{
SensorValue[rightencoder] = 0;
SensorValue[leftencoder] = 0;

float drivegoal = (inches * 49.9109902106);

while(abs(SensorValue[rightencoder] > drivegoal || SensorValue[leftencoder] > drivegoal))
{
	motor[rightmotor] = -power;
	motor[leftmotor] = -power;
}

{
	motor[rightmotor] = 0;
	motor[leftmotor] = 0;
}

}

void Turn(void direction, int degrees, int power)
{
SensorValue[rightencoder] = 0;
SensorValue[leftencoder] = 0;

float turngoal = (degrees * 6.09777777778)

switch(direction)
{
	case right:
		greaterthan = leftencoder;
		lessthan = rightencoder;
		negative = rightmotor;
		positive = leftmotor;
	break;
	case left:
	 	greaterthan = rightencoder;
	 	lessthan = leftencoder;
	 	negative = leftmotor;
	 	positive = rightmotor;
	break;
	default:
}

while(abs(SensorValue[greaterthan] > turngoal || SensorValue[lessthan] < -turngoal))
{
	motor[negative] = -power;
	motor[positive] = power;
}

{
	motor[leftmotor] = 0;
	motor[rightmotor] = 0;
}

}

// if(direction == Right)
// {
// while(abs(SensorValue[leftencoder] > turngoal || SensorValue[rightencoder] < -turngoal))
// {
// motor[rightmotor] = -power;
// motor[leftmotor] = power;
// }
//
// {
// motor[leftmotor] = 0;
// motor[rightmotor] = 0;
// }
// }
//
// if(direction == Left)
// {
// while(abs(SensorValue[rightencoder] > turngoal || SensorValue[leftencoder] < -turngoal))
// motor[rightmotor] = power;
// motor[leftmotor] = -power;
// }
//
// {
// motor[leftmotor] = 0;
// motor[rightmotor] = 0;
// }
// }
//}

void move(void xdirection, float amount, int power) //New
{
switch(xdirection)
{
case forward:
driveForward(amount, power);
break;
case left:
Turn(xdirection, amount, power);
break;
case right:
Turn(xdirection, amount, power);
break;
case backward:
driveBackward(amount, power);
break;

}

}

task main() //Use move(direction, amountif direction = left/right degrees, if direction = forward/backward use inches)
{
//Put code in here

}

Well, you should at least try to compile the code before posting it. This won’t even compile, I’d say.

Anyway, this loop in driveBackward is wrong:


while(abs(SensorValue[rightencoder] > drivegoal || SensorValue[leftencoder] > drivegoal))

First, you’re doing abs() on the boolean result of the check, I suppose you wanted to take abs value of the sensor.
Even then, your comparison is backwards, the condition will start as false and the loop would never run. The absolute distance condition doesn’t change just because you are driving back…

All of your loops seem to have the same issue that @nenik mentioned. Remove the absolute value checking; it isn’t necessary in any (I think) of the loops. The


default

in your


switch

in


Turn()

does not have a


break

. I’m pretty sure it will still work how it is, since


default

is the last entry of the


switch

, but it is technically incorrect. Since you don’t do anything there anyway, I’d suggest removing the


default

entirely.

Sorry, well I am working on a computer that isn’t running windows, so I wasn’t programming in robotC, I was just using a text editor and saving the file as a .c so I want able to compile

@nenik @ZackDaChampion
I just fixed what you two had found… sorry for not having a compiled program… I am just not able to until I can actually find a computer equipped with robotC which will probably take a couple of days.
So can you tell me if this looks right now, thanks:

void driveForward(float inches, int power)
{
SensorValue[rightencoder] = 0;
SensorValue[leftencoder] = 0;

int drivegoal = (inches * 49.9109902106);

while(SensorValue[rightencoder] < drivegoal || SensorValue[leftencoder] < drivegoal)
{
	motor[rightmotor] = power;
	motor[leftmotor] = power;
}

{
	motor[rightmotor] = 0;
	motor[leftmotor] = 0;
}

}

void driveBackward(float inches, int power) //New
{
SensorValue[rightencoder] = 0;
SensorValue[leftencoder] = 0;

float drivegoal = (inches * 49.9109902106);

while(SensorValue[rightencoder] > drivegoal || SensorValue[leftencoder] > drivegoal)
{
	motor[rightmotor] = -power;
	motor[leftmotor] = -power;
}

{
	motor[rightmotor] = 0;
	motor[leftmotor] = 0;
}

}

void Turn(void direction, int degrees, int power)
{
SensorValue[rightencoder] = 0;
SensorValue[leftencoder] = 0;

float turngoal = (degrees * 6.09777777778)

switch(direction)
{
	case right:
		greaterthan = leftencoder;
		lessthan = rightencoder;
		negative = rightmotor;
		positive = leftmotor;
	break;
	case left:
	 	greaterthan = rightencoder;
	 	lessthan = leftencoder;
	 	negative = leftmotor;
	 	positive = rightmotor;
	break;
}

while(SensorValue[greaterthan] > turngoal || SensorValue[lessthan] < -turngoal)
{
	motor[negative] = -power;
	motor[positive] = power;
}

{
	motor[leftmotor] = 0;
	motor[rightmotor] = 0;
}

}

// if(direction == Right)
// {
// while(abs(SensorValue[leftencoder] > turngoal || SensorValue[rightencoder] < -turngoal))
// {
// motor[rightmotor] = -power;
// motor[leftmotor] = power;
// }
//
// {
// motor[leftmotor] = 0;
// motor[rightmotor] = 0;
// }
// }
//
// if(direction == Left)
// {
// while(abs(SensorValue[rightencoder] > turngoal || SensorValue[leftencoder] < -turngoal))
// motor[rightmotor] = power;
// motor[leftmotor] = -power;
// }
//
// {
// motor[leftmotor] = 0;
// motor[rightmotor] = 0;
// }
// }
//}

void move(void xdirection, float amount, int power) //New
{
switch(xdirection)
{
case forward:
driveForward(amount, power);
break;
case left:
Turn(xdirection, amount, power);
break;
case right:
Turn(xdirection, amount, power);
break;
case backward:
driveBackward(amount, power);
break;

}

}

task main() //Use move(direction, amountif direction = left/right degrees, if direction = forward/backward use inches)
{
//Put code in here

}

backward still wrong:


SensorValue[rightencoder] = 0;
float drivegoal = (inches * 49.9109902106);
...]
while(SensorValue[rightencoder] > drivegoal || SensorValue[leftencoder] > drivegoal)

Your SensorValue is initially zero and should go into negative values, but your goal is positive, thus the loop never runs, motors never start.
But the structure would work, just fix the sign:


float drivegoal = -(inches * 49.9109902106);

In your Turn, you don’t declare the variables (missing variable types), but it’s unnecessary complicated - just keep around a single int that would either be 1 or -1 based on the direction, use it to normalize the values.

I just changed some of the code, I kept in mind your ideas @nenik but slightly modified them to make the code more efficient so I wouldn’t have to have two separate forwards and backwards driving functions. So if you could see any errors now, that would be great!:

void driveDirection(void ydirection float inches, int power)
{
SensorValue[rightencoder] = 0;
SensorValue[leftencoder] = 0;
int dir = 0;

switch(ydirection)
{
case forward:
int dir = 1;
break;

case backward;
int dir = -1;
break:

default:
int dir = 1;
break;

}
int drivegoal = (inches * 49.9109902106);

while(SensorValue[rightencoder] * ydir < drivegoal || SensorValue[leftencoder] * ydir < drivegoal)
{
	motor[rightmotor] = (dir * power); 
	motor[leftmotor] = (dir * power);
}

{
	motor[rightmotor] = 0;
	motor[leftmotor] = 0;
}

}

//void driveBackward(float inches, int power) //New
//{
// SensorValue[rightencoder] = 0;
// SensorValue[leftencoder] = 0;
//
// float drivegoal = -(inches * 49.9109902106);
//
// while(SensorValue[rightencoder] > drivegoal || SensorValue[leftencoder] > drivegoal)
// {
// motor[rightmotor] = -power;
// motor[leftmotor] = -power;
// }
//
// {
// motor[rightmotor] = 0;
// motor[leftmotor] = 0;
// }
//}

void Turn(void direction, int degrees, int power)
{
SensorValue[rightencoder] = 0;
SensorValue[leftencoder] = 0;
int dir = 0

switch(direction)
{
	case right:
		dir = 1;
	break;
	case left:
		dir = -1;
	break;
}

float turngoal = (degrees * 6.09777777778);

while(SensorValue[leftencoder] * dir < turngoal || SensorValue[rightencoder] * dir > -turngoal)
{
	motor[leftmotor] = dir * power;
	motor[rightmotor] = -dir * power;
}

{
	motor[leftmotor] = 0;
	motor[rightmotor] = 0;
}

}

// if(direction == Right)
// {
// while(abs(SensorValue[leftencoder] > turngoal || SensorValue[rightencoder] < -turngoal))
// {
// motor[rightmotor] = -power;
// motor[leftmotor] = power;
// }
//
// {
// motor[leftmotor] = 0;
// motor[rightmotor] = 0;
// }
// }
//
// if(direction == Left)
// {
// while(abs(SensorValue[rightencoder] > turngoal || SensorValue[leftencoder] < -turngoal))
// motor[rightmotor] = power;
// motor[leftmotor] = -power;
// }
//
// {
// motor[leftmotor] = 0;
// motor[rightmotor] = 0;
// }
// }
//}

void move(void xdirection, float amount, int power) //New
{
switch(xdirection)
{
case forward:
driveDirection(xdirectoin, amount, power);
break;
case left:
Turn(xdirection, amount, power);
break;
case right:
Turn(xdirection, amount, power);
break;
case backward:
driveDirection(xdirectoin, amount, power);
break;

}

}

task main() //Use move(direction, amountif direction = left/right degrees, if direction = forward/backward use inches)
{
//Put code in here

}

I just looked over it again, and I think it looks good but I could use a second opinion

Yes, you’re getting there (sans few syntax errors that you’ll quickly catch once at RobotC).
If you really want to use direction as an enum, you’d have to delare it as a type and pass it that way, not as void, so just for completness, it would look like (w/o compiler at hand):


typedef enum {
    forward,
    backward
} direction;

void driveDirection(direction ydirection, float inches, int power) {
...]
switch(ydirection) {
    case forward:


But why bother? You could easier go with:


void driveDirection(float inches, int power) {
  int drivegoal = (inches * 49.9109902106);
  int dir = 1;

  if (inches < 0) {
    dir = -1;
  }

  SensorValue[rightencoder] = 0;
  SensorValue[leftencoder] = 0;

  while(dir*SensorValue[rightencoder] < dir*drivegoal || dir*SensorValue[leftencoder] * dir < drivegoal) {
    motor[rightmotor] = (dir * power);
    motor[leftmotor] = (dir * power);
  }

  motor[rightmotor] = 0;
  motor[leftmotor] = 0;
}