Log in

View Full Version : Would this code work for kaj drive?


tahmid0517
16-05-2015, 23:00
Hi, I'm new to programming and I wrote a piece of code that I don't have access to a robot right now to test on:

frontRight.set(stick1.getY() - stick2.getX());
backRight.set(stick1.getY() - stick2.getX());
frontLeft.set(stick1.getY()+ stick2.getX());
backLeft.set(stick1.getY()+ stick2.getX());

Assuming all the motor and joystick ports are declared correctly and this code is in the teleop periodic section, would this code work as a form of kaj drive where a robot's forward and backward motion is controlled with the left joystick and turning is controlled by the right joystick?

BigJ
17-05-2015, 01:21
It "would" work - however if the expression inside set() goes above 1.0 it will be cut off. It is worth thinking about how that will affect your controls.

GeeTwo
17-05-2015, 10:25
The easiest way to scale to prevent set values outside of the -1 to 1 range would be to divide everything by 2. Unfortunately, this would reduce your maximum forward speed (and the others as well) by 50%.
It would also be cleaner to get the joystick numbers once rather than call the get() methods repeatedly.
A fairly general way to combine the numbers which allows full speed but prevents bounds errors on the set() methods could be:


fwd = stick1.getY();
rot = stick2.getX();
scale = Math.max(1.0, Math.abs(fwd) + Math.abs(rot);
frontRight.set((fwd - rot) / scale);
backRight.set((fwd - rot) / scale);
frontLeft.set((fwd + rot) / scale);
backLeft.set((fwd + rot) / scale);

Ether
17-05-2015, 14:19
...or you could just use the arcade drive in the Java WPILib and provide the fwd/rev from the left Y; and the turning from the right X