# Differential Drive

The Differential Drive module provides a quick and easy way to convert from a forward/backward and left/right input into a left forward/backward and right forward/backward drive. Differential drive is most common in two wheeled robots which are controlled by two servos or motors and have forward and reverse capabilities. The module solves the problem of converting from one directional specification into a differential one.

## Instructions

1. Forward/Backward Input - The variable that contains the value represents the forward and reverse movement of the robot. The minimum and maximum values define the full range of what those values can be. Typically, the higher this value the faster the robot should move, the lower the slower. Negative values or values below the mid point would cause the robot to move in reverse.

2. Left/Right Input - The variable that contains the value represents the left and right movement of the robot. The minimum and maximum values define the full range of what those values can be. The extremes are defined as the robot spinning in one direction versus the other. Values close to the mid point would stop the robot turning.

3. Left and Right Motors - The variables that WILL contain the value for each motor. The minimum and maximum values specify the full range of possible values for that motor and can be different for left and right sides (but not usually). These values are then used in other motor drive modules like the Dimension Engineering Sabertooth motor drive or the Pololu Maestro servo controller (to name a few) which would send that value straight to the motor or servo which would cause the robot to move.

4. Invert Forward/Backward - If you find that the robot is moving forward when it should be backward select the "Swap Left and Right Motors" checkbox. This will invert the Forward/Backward input (i.e. joy_y) to cause the robot to move in the opposite direction.

5. Invert Left/Right - If you find that the input forward/backward direction (i.e. joy_y) is moving forward when it should be backward select the "Swap Left and Right Motors" checkbox.

6. Oppose Left/Right - If you find that you are moving forward when you should be moving left or right it is possible that the motors/servos were not mounted opposing each other. I.e. to move forward the values need to be opposite each other (-255, 255) instead of the same (255, 255). Selecting the Oppose checkbox will correct this issue but will then likely need to have Inverted Left/Right or Forward/Backward changed too.

7. Predictive Behaviour - As soon as the input values disappear the motor values will immediately become neutral (i.e. stop). To avoid this and have the motor values slowly approach netural you can enable the motor value Decay which will decrease the values towards neutral at the specified rate. This allows you to add some lag into the system which causes the robot to continue to move in its last direction for a while longer in hopes to reacquire a lost target that has moved off screen.

Note, the decay value will linearly decrease the values over each frame where the input values are empty (i.e. undefined/removed or have no value/empty string/"". This is different than a zero 0 value.)

## Notes

The code used to perform the conversion in VBScript is

```' amount of y joystick determines speed
speed = 128 - CInt(((GetVariable("joy_y") * 128) / 1000))

' amount of x joystick determines rotation
turn = CInt(((GetVariable("joy_x") * 128) / 1000))

' determine intermediate values
mleft = speed + turn
mright = speed - turn

'ensure they are within the range 0 - 255
if mleft<0 then mleft=0
if mright<0 then mright=0
if mleft>255 then mleft=255
if mright>255 then mright=255

' finally set the variables to be used in the joystick module
SetVariable "left_motor", mleft
SetVariable "right_motor", mright
```

## Example

Click Here to download a robofile that shows how to use the Differential Drive module with a joystick and Create Robot.