element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Members
    Members
    • Benefits of Membership
    • Achievement Levels
    • Members Area
    • Personal Blogs
    • Feedback and Support
    • What's New on element14
  • Learn
    Learn
    • Learning Center
    • eBooks
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • Experts & Guidance
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Dev Tools
    • Manufacturers
    • Raspberry Pi
    • RoadTests & Reviews
    • Avnet Boards Community
    • Product Groups
  • Store
    Store
    • Visit Your Store
    • 'Choose another store...'
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
Personal Blogs
  • Members
  • More
Personal Blogs
Legacy Personal Blogs Beaglebot Wii Remote
  • Blog
  • Documents
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Blog Post Actions
  • Subscribe by email
  • More
  • Cancel
  • Share
  • Subscribe by email
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: Former Member
  • Date Created: 25 Aug 2015 3:05 PM Date Created
  • Views 382 views
  • Likes 0 likes
  • Comments 0 comments
Related
Recommended

Beaglebot Wii Remote

Former Member
Former Member
25 Aug 2015

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.

image

 

 

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!


You don't have permission to edit metadata of this video.
Edit media
x
image
Upload Preview
image

 

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.

  • Sign in to reply
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2023 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube