View Single Post
  #7   Spotlight this post!  
Unread 23-03-2008, 12:59
tajmorton tajmorton is offline
Registered User
FRC #0847 (PHRED)
Team Role: College Student
 
Join Date: Nov 2007
Rookie Year: 2008
Location: Philomath, OR
Posts: 10
tajmorton is a jewel in the roughtajmorton is a jewel in the roughtajmorton is a jewel in the roughtajmorton is a jewel in the rough
Send a message via AIM to tajmorton
Re: control all driving with one joystick

What are you using to program your robot? easyC, the default code, WPILib...?

This year we experimented with two different drive modes: arcade mode and RC-Car style drive (two joysticks, one for forward/backward and one for steering).

If you're using WPILib or easyC, arcade mode for 2 wheel drive is fairly easy to do. I don't have experience with easyC, but with WPILib/MPLab, you need to call Arcade2() function in your OperatorControl(). Unfortunately, I appear to have lost the code that I used for it, but check the docs (page 41) for more information. Basically, you just need to pass Arcade2 the joystick port you're using (PORT_1, PORT_2, etc), the axis of the joystick (X_AXIS, Y_AXIS), the PWM ports that your victors are plugged into (1, 2, etc), and if either your left or right motor is inverted (1=inverted, 0=not, I think).

If you're not using WPILib, you can still do the mixing of the joysticks manually and achieve the same result. This is what we ended up doing because we wanted more control over how sensitive the joysticks were (and so we could use a gyro to keep our bot driving in a straight line).

Our final code worked something like this:
  1. Read Joystick Values
  2. Processed the joystick values through a function that mixed the X and the Y into PWM values for our 2 motors
  3. Sent the motor values to the PWMs

This is what our code that processes joystick values into motor values sort of looks like:
Code:
/* OurDrive
 * Takes joystick x and y and converts into motor values. 
 * Then drives motors.
 * WARNING: Unlike the numbers you get from the joysticks,
  x and y are signed (-127 - 127). Also, the x val is expected to
  go from -127 (full left) to 127 (full right). Make sure you convert
  the joystick vals to signed and multiply x by -1.
*/
void OurDrive(int x, int y) {
	int rm, lm; // values to send to motors
	if ((x==0) && (y==0)) { //not going anywhere
		rm=0;
		lm=0;
	}
	else if (x==0) { //driving straight
		rm = lm = y;
	}
	else if (y==0) { //spin turn
		rm=(x*-1)/2;
		lm=x/2;
	}
	else if (x<0 && y>0) {//forward left,, y pos & x neg
		if (y-x > 127){//turn by decrease
			rm= y;
			lm=y+x;
		}
		else{//turn by increase
			rm=y-x;
			lm=y;
		}
	}
	else if (x<0 && y<0) {//back left, y neg & x neg
		if (y-x < -127) { //turn by decrease
			rm=y-x;
			lm=y;
		}
		else { //turn by increase
			rm=y;//drive backwards
			lm=y+x;//add neg x to neg y, lm goes back faster
		}
	}
	else if (x>0 && y>0) {//forward right, y pos & x pos
		if (y+x>127) {//turn by decrease
			lm = y;
			rm = y-x;
		}
		else {//turn by increase
			lm = y+x;
			rm = y;
		}
	}
	else if (x>0 && y<0) {//back right, y neg & x pos
		if (y-x<-127){//turn by decrease
			rm= y;
			lm=y+x;
		}
		else {//turn by increase
			rm=y-x;
			lm=y;
		}
	}
	
	/* now, drive your motors with the PWM values of lm and rm. In WPILib, you use Motors(lm,rm);
	Make sure you convert the numbers to unsigned if you drive the PWMs directly */
}
Now, just read the x and y axis from the same joystick and send it to the OurDrive() function. You will probably want to process the joystick variables somehow to make the drive more controllable. We gave the drivers the option of dividing by 4 or 6 (selectable by pressing a button on a joystick) to make for a fairly controllable robot.

Good luck! If you have any other questions, please ask!
- Taj