Drive code not working; any suggestions?

We are testing code for our motors/servos. Apparently after downloading the code to our cRIO, nothing seems to function. It builds, so we are sure it is not a syntax error.
The code is as follows:

#include "WPILib.h"

void update_drive;

class RobotDemo : public SimpleRobot 
{ 
	RobotDemo(void)
	{ 	// put initialization code here
		pwm leftmotor(1);
		pwm rightmotor(2);
		joystick leftjoy(1);
		joystick rightjoy(2);
	}
	void Autonomous(void)
	{ 	// put autonomous code here
		
	}
	void OperatorControl(void)
	{ 	// put operator control code here
		update_drive();
	}
};


	start_robot_class(RobotDemo);


void update_drive()
{
	int k = /*your value here*/;		//speed increase/decrease value
	
	if (leftjoy:gety() >= (255 - k))		//if left joystick is near max
	{	//overshot security
		leftmotor:rawset(leftjoy:gety());
	}
	
	if (leftjoy:gety() <= k)				//if left joystick is near min
	{	//undershot security
		leftmotor:rawset(leftjoy:gety());
	}
	
	if ((leftjoy:gety() < (255 - k)) && (leftjoy:gety() > k))		//otherwise...
	{	//standard control
		if (leftjoy:gety() > leftmotor:rawget())
		{	//speed up
			int leftmotortemp1 = leftmotor:rawget();
			leftmotor:rawset(leftmotortemp1 + k);
			if (leftjoy:gety() < leftmotor:rawget())
			{	//overshot security
				leftmotor:rawset(leftjoy:gety());
			}
		}
		if (leftjoy:gety() < leftmotor:rawget())
		{	//slow down
			int leftmotortemp1 = leftmotor:rawget();
			leftmotor:rawset(leftmotortemp1 - k);
			if (leftjoy:gety() > leftmotor:rawget())
			{	//undershot security
				leftmotor:rawset(leftjoy:gety());
			}
		}
	}
	
	
	
	
	if (rightjoy:gety() >= (255 - k))		//if right joystick is near max
	{	//overshot security
		rightmotor:rawset(rightjoy:gety());
	}
	
	if (rightjoy:gety() <= k)				//if right joystick is near min
	{	//undershot security
		rightmotor:rawset(rightjoy:gety());
	}
	
	if ((rightjoy:gety() < (255 - k)) && (rightjoy:gety() > k))		//otherwise...
	{	//standard control
		if (rightjoy:gety() > rightmotor:rawget())
		{	//speed up
			int rightmotortemp1 = rightmotor:rawget();
			rightmotor:rawset(rightmotortemp1 + k);
			if (rightjoy:gety() < rightmotor:rawget())
			{	//overshot security
				rightmotor:rawset(rightjoy:gety());
			}
		}
		if (rightjoy:gety() < rightmotor:rawget())
		{	//slow down
			int rightmotortemp1 = rightmotor:rawget();
			rightmotor:rawset(rightmotortemp1 - k);
			if (rightjoy:gety() > rightmotor:rawget())
			{	//undershot security
				rightmotor:rawset(rightjoy:gety());
			}
		}
	}
}

Any suggestions would be greatly appreciated. Our team is really falling behind this year, and nothing seems to be going right.

Thanks,
iPirates 1528

When you say “nothing seems to function”, do you mean it doesn’t respond AT ALL, or does it not do exactly what you want? There’s a big difference.

Also, just because you have no syntax issues, doesn’t mean the cRio is happy. Did you use an RS-232 link to the cRIO to see what the response was after the download? Did you reboot after download?

Does the control box say “no code”?

These details are critical.

It does not respond at all…

No, we did not use the RS-232 link, but we did reboot after uploading the code.

And yes, the control box says “No Code”

We re-imaged the C-Rio, and the code that that it put in worked, so it’s not a hardware problem.

You’re not building the right thing apparently, since theres no way that code will build as pasted. For starters, none of your types are correct (joystick instead of Joystick, pwm instead of PWM). Second, you need to have your update_drive() function inside the class definition.

And… the list goes on. Start with the SimpleTemplate, and make simple changes first. Then work your way up.

If you are not deploying the code, then you should not reboot otherwise the code goes away on reboot.

I’m really surprised that this compiles… are you sure you’re building the correct project?

One thing I do notice is that the OperatorControl code is only going to execute once. You need to do something like this:


void OperatorControl()
{
     while (IsOperatorControl())
     {
            update_drive();
     }
}

Additionally, your joysticks and PWM objects need to be declared as member variables within the class, rather than local variables in the class constructor. As it is in your code right now, leftmotor, rightmotor, leftjoy, rightjoy are all only available to use in the constructor.

I’d also recommend against using the PWM class directly. Instead you should use Victor or Jaguar.

Also, take note that the Joystick class will return a float from -1.0 to +1.0 when you look at an axis, not an 8-bit uint from 0-255.

You should have something more like this:


#include "WPILib.h"

class MyRobot : public SimpleRobot
{
    Victor leftMotor(1);
    Victor rightMotor(2);
    Joystick leftStick(1);
    Joystick rightStick(2);

    void Autonomous()
    {
        // Autonomous code goes here.
    }
    void OperatorControl()
    {
           while (IsOperatorControl())
           {
                // Drive code goes here.
           }
     }
};

START_ROBOT_CLASS(MyRobot);

Hope this is enough to get you started.

Others have posted since I started writing this, but I’ll post since I had a few details that the others didn’t include.

As has been stated, I’m not sure how there aren’t syntax errors.

You have many capitalization errors in your post (maybe it was a poor transcription?). For example, you have pwm when it should be PWM. This brings up the question of why you’re using PWM directly and not going through the Victor or Jaguar class?

Your function prototype of update_drive should be giving you an error. You have

void update_drive;

when it needs to be

void update_drive(void);

The way you’re trying to access class members is incorrect and should be giving you an error. You have

leftjoy:gety()

when it should be

leftjoy.GetY()

And what’s with this line? The compiler would most definitely complain about that

int k = /*your value here*/;		//speed increase/decrease value

In summary, the code that you posted should not compile. Double check that you’re not getting errors.

We’re definately having some communication issues between programmers. We’re currently working on correcting all these errors (and we didn’t notice the blank variable declarations >.<) and we’ll post the new code as soon as we test it. Thanks for the help :wink:

We took all of your suggestions into consideration, and here’s our revised code. It’s still not building and maybe we were wrong about the syntax error. :o

#include <WPILib.h>
#include <Jaguar.h>
#include <Victor.h>



class RobotDemo : public SimpleRobot 
{ 
	RobotDemo(void)
	{ 	// put initialization code here
		Jaguar leftmotor(1);
		Victor rightmotor(2);
		Joystick leftjoy(1);
		Joystick rightjoy(2);
	}
	void Autonomous(void)
	{ 	// put autonomous code here
		
	}
	void OperatorControl(void)
	{ 	// put operator control code here
		update_drive();
	}
	
	
	void update_drive()
	{
	int k = 1;		//speed increase/decrease value
	
	if (leftjoy.GetY() >= (255 - k))		//if left joystick is near max
	{	//overshot security
		leftmotor.rawset(leftjoy.GetY());
	}
	
	if (leftjoy.GetY() <= k)				//if left joystick is near min
	{	//undershot security
		leftmotor.rawset(leftjoy.GetY());
	}
	
	if ((leftjoy.GetY() < (255 - k)) && (leftjoy.GetY() > k))		//otherwise...
	{	//standard control
		if (leftjoy.GetY() > leftmotor.rawget())
		{	//speed up
			int leftmotortemp1 = leftmotor.rawget();
			leftmotor.rawset(leftmotortemp1 + k);
			if (leftjoy.GetY() < leftmotor.rawget())
			{	//overshot security
				leftmotor.rawset(leftjoy.GetY());
			}
		}
		if (leftjoy.GetY() < leftmotor.rawget())
		{	//slow down
			int leftmotortemp1 = leftmotor.rawget();
			leftmotor.rawset(leftmotortemp1 - k);
			if (leftjoy.GetY() > leftmotor.rawget())
			{	//undershot security
				leftmotor.rawset(leftjoy.GetY());
			}
		}
	}
	
	
	
	
	if (rightjoy.GetY() >= (255 - k))		//if right joystick is near max
	{	//overshot security
		rightmotor.rawset(rightjoy.GetY());
	}
	
	if (rightjoy.GetY() <= k)				//if right joystick is near min
	{	//undershot security
		rightmotor.rawset(rightjoy.GetY());
	}
	
	if ((rightjoy.GetY() < (255 - k)) && (rightjoy.GetY() > k))		//otherwise...
	{	//standard control
		if (rightjoy.GetY() > rightmotor.rawget())
		{	//speed up
			int rightmotortemp1 = rightmotor.rawget();
			rightmotor.rawset(rightmotortemp1 + k);
			if (rightjoy.GetY() < rightmotor.rawget())
			{	//overshot security
				rightmotor.rawset(rightjoy.GetY());
			}
		}
		if (rightjoy.GetY() < rightmotor.rawget())
		{	//slow down
			int rightmotortemp1 = rightmotor.rawget();
			rightmotor.rawset(rightmotortemp1 - k);
			if (rightjoy.GetY() > rightmotor.rawget())
			{	//undershot security
				rightmotor.rawset(rightjoy.GetY());
			}
		}
	}
	}
};


	Start_Robot_Class(RobotDemo);

Edit: The code has been corrected for the most part. Please let us know if you can find any other problems.

Some advice - when something isn’t compiling and you ask for help, provide the people helping with the compiler error. Most of the time someone with an experience can provide a ton of help just by seeing that output.

I highly suggest that you start to learn how to read the errors that a compiler is giving you. It will save you a ton of time if you can start understanding what it is actually telling you. One hint is to always start at the first error. More than likely, by cleaning up the first error you will inherently fix some of the ones below it.

Now, on to the problems in this revision. Go back and compare your code to that in MattD’s above. There are three major things that are different.

  1. He has his objects as members of his class, not local variables in his constructor like you do.
  2. Your constructor is taking a void argument. That isn’t correct syntax. For constructors you need to have empty parenthesis following the classname (i.e RobotDemo())
  3. C and C++ are case sensitive languages. Therefore your Start_Robot_Class is not the same as the macro that is actually defined START_ROBOT_CLASS