A Beagle Bone Black Bot controlled over bluetooth using a Wii-Mote
So my third and final Bot in the series. This time controlled using a Wii-mote over bluetooth. I came across a python module that allowed any linux system to communicate with a Wii-Mote using a bluetooth adapter. The module seemed very easy to use and very intuitive, and since I was doing the other bots at the time it seemed a logical use for it as I had all the hardware already built.
The chassis and electronics are all the same as my first bot, the WiFi controlled BBot (Ben). So head over THERE for instructions on how to build the electronics and everything.
You should start with something looking like this ↓ it will need two servos. One connected to pin8_13 and the other to pin9_42, you will also need a bluetooth dongle, I used order code 2102735 and it worked perfectly.
The Code
The code is the only thing that is different. It is easy to understand and edit
Dependencies
You will need to install a few things to start with, do this using the following command.
apt-get install bluetooth python-cwiid
this will install the bluetooth package and the cwiid module for python (the module that allows us to connect to the Wii-Remote).
The Code
The code is very simple, just one python script.
For those of you who have looked at or followed my blog on the WiFi Bot then you will see I have taken a lot of the code from there and only actually changed the bottom section.
To open a file type the following command (I am using nano here as it is easier to understand if you are not familiar with linux)
nano ~/bt_bot.py
Into this file carefully type the following
#P8_13 Servo 1
#P9_14 Servo 2
servo1="P8_13"
servo2="P9_42"
import Adafruit_BBIO.PWM as PWM #imports the required python libraries, including the PWM libraries to control the servos
import time
import cwiid
duty_min = 92.4 #sets the min and max duty cycle for the servos
duty_max = 99.9
duty_span = duty_max - duty_min #calculates the range between the min and max duty cycles
def duty(percent): #when called, this function converts percentage of power (0=full reverse, 50=stop, 100=full forward) to the actual duty cycle
return(100 - ((float(percent) / 100) * duty_span + duty_min))
# Functions for moving
# when called they correct the servo duty cycle
# the duty function is called so we can use % instead of actuall duty cycle values to make it easier to interpret the speed of the servos from the code
def forward():
PWM.set_duty_cycle(servo2, duty(100))
PWM.set_duty_cycle(servo1, duty(0))
return()
def left():
PWM.set_duty_cycle(servo1, duty(0))
PWM.set_duty_cycle(servo2, duty(0))
return()
def right():
PWM.set_duty_cycle(servo2, duty(100))
PWM.set_duty_cycle(servo1, duty(100))
return()
def backward():
PWM.set_duty_cycle(servo2, duty(0))
PWM.set_duty_cycle(servo1, duty(100))
return()
def stop():
PWM.set_duty_cycle(servo1, duty(50))
PWM.set_duty_cycle(servo2, duty(50))
return()
def php_control():
#### Main Run Loop ####
PWM.start(servo1, 95.0, 60) # initiates the servos
PWM.start(servo2, 95.0, 60)
while 1:
file = open("/var/www/move.txt", "r") # opens the text file controlled from the php script
try: # tries to read the text file
val = int(file.readline());
except ValueError: #if python can not read the text file it shall return -1 (error code)
val = -1
file.close() # closes text file
if val == 2: #if direction is back
backward();
elif val == 4: #if direction is left
left();
elif val == 6: #if direction is right
right();
elif val == 8:#if direction is forward
forward();
else: #if direction is not front, back, L, or R
stop(); #then stop moving
#### Loop ####
def line_follow():
### MAIN RUN LOOP ###
PWM.start(servo1, 95.0, 60) # initiates the servos
PWM.start(servo2, 95.0, 60)
while 1:
### check line follow pin ###
### if both sensors high set val to 2 ###
### if right sensor is high set val to 6 ####
### elif left sensor is low set val to 4 ###
### elif both sensors low set val to 8 ###
### set val ###
if val == 2: #if direction is back
backward()
elif val == 4: #if direction is left
left()
elif val == 6: #if direction is right
right()
elif val == 8:#if direction is forward
forward()
else: #if direction is not front, back, L, or R
stop() #then stop moving
#### Loop ####
def wii_control():
PWM.start(servo1, 95.0, 60) # initiates the servos
PWM.start(servo2, 95.0, 60)
while True:
try:
print "Press the 1+2 buttons on your Wii-mote to connect"
time.sleep(1)
wm=cwiid.Wiimote()
break
except RuntimeError:
continue
while True:
print "Wii-mote connected"
Rumble=False
wm.rpt_mode=cwiid.RPT_BTN
while True:
if wm.state['buttons']==512:
forward();
elif wm.state['buttons']==1024:
right()
elif wm.state['buttons']==256:
backward()
elif wm.state['buttons']==2048:
left()
elif wm.state['buttons']==4096:
break
else:
stop()
time.sleep(0.2)
PWM.start(servo1, 95.0, 60) # initiates the servos
PWM.start(servo2, 95.0, 60)
stop()
wii_control()
Finished
Plug in your BT dongle.
Then boot your board, Login, then type this and your off
sudo python ~/bt_bot.py
You will need to press the 1 and 2 buttons on your wii mote when you run the script to connect the wii mote to your BBB, then use the Dpad to control your bot.
You're off!
Please leave comments and suggestions below. Also if you have any problems then please shout up and give me as much detail as possible, then we can try help you out.
