Baby you can drive my car (with a bit of Python)
Tharshan Muthulingam
Bit About me
- Final Year Computer Systems Engineering Student
- Studying in Cardiff University
Equipment
mbed micrcontroller
ps3 controller
laptop
breadboard, wires, optoisolators
mbed
-
A microcontroller developed by a team at ARM
-
Similar to the Arduino
- Great for hobbyist projects and good for when you want to take it to the next step
- Microcontrollers are for rapid prototyping
Opto-isolators
-
To prevent high voltages from affecting the system receiving the signal.
-
In our case, the remote for the car is receiving the signal.
The big picture
ps3 controller usb packets
Using libusb we can read the usb packets in python
We need to find the right data values for a forward and backward motion, and also turning left and right.
- Using joysticks
- SixAxis motion
Python code
import usb
import serial
import pdb
ps3 = usb.core.find(find_all=True, idVendor=0x054C, idProduct=0x0268)[0]
ps3.set_configuration()
ser = serial.Serial('/dev/tty.usbmodem1422', 115200, timeout=0.1)
if __name__ == "__main__":
while True:
data = ps3.ctrl_transfer(0xa1, 0x1, 0x0101, 0, 0x31)
# 0-90 and 255-135
# print "Triangle %03d Cross %03d Left JoyStick %03d" % (data[22], data[24], data[42])
ser.write("%03d%03d%03d" % (data[22], data[24], data[6]))
Mapping Controls
Triangle Button for backwards, X button for forwards
Left Joystick for left and right
Though trial and error....
We find the index in the usb packet that map to those actions
Next finding Threshold Values...
Thresholds
Threshold for the button is easy
0 for not pressed and greater than 0 for pressed
We want to use the Left Joystick to turn
Wiring it up
PWM
- 4 PWM Outputs and 1 Serial Output
-
What is PWM?
-
Pulse width modulation (PWM) is a simple method
of using a rectangular digital waveform to control an
analog variable
-
PWM duty cycle is changed based on ps3 controller values
Programming the mbed
- Online IDE, Plug and play, C++ SDK
PwmOut forward(p26);
forward.period(0.02);
if (x_btn > 50){
back = (float(x_btn-128)/128);
}else if (triangle_btn > 50){
forward = (float(triangle_btn-128)/128);
}
if (xaxis > 120 and xaxis < 135){
left=0;
right=0;
}else if (xaxis > 130){
left = (float(xaxis-128)/128);
}else{
right = float(abs(1-float(xaxis)/128));
}
DEMO TIME!
Thanks for listening
Twitter: @viperfx09
Email: tharshan09@gmail.com
Github: www.github.com/viperfx
Baby you can drive my car (with a bit of Python)
By Tharshan Muthulingam
Baby you can drive my car (with a bit of Python)
- 3,411