├── wii_remote_test.py ├── wii_controller.py └── README.md /wii_remote_test.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | #+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 3 | #|R|a|s|p|b|e|r|r|y|P|i|-|S|p|y|.|c|o|.|u|k| 4 | #+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 5 | # 6 | # wii_remote_1.py 7 | # Connect a Nintendo Wii Remote via Bluetooth 8 | # and read the button states in Python. 9 | # 10 | # Project URL : 11 | # http://www.raspberrypi-spy.co.uk/?p=1101 12 | # 13 | # Author : Matt Hawkins 14 | # Date : 30/01/2013 15 | 16 | # ----------------------- 17 | # Import required Python libraries 18 | # ----------------------- 19 | import cwiid 20 | import time 21 | #import RPi.GPIO as io 22 | 23 | #io.setmode(io.BCM) 24 | #pins = (2,3,4,17) 25 | #for i in pins: 26 | # io.setup(i,io.OUT) 27 | 28 | button_delay = 0.1 29 | 30 | print 'Press 1 + 2 on your Wii Remote now ...' 31 | time.sleep(1) 32 | 33 | # Connect to the Wii Remote. If it times out 34 | # then quit. 35 | try: 36 | wii=cwiid.Wiimote() 37 | except RuntimeError: 38 | print "Error opening wiimote connection" 39 | quit() 40 | 41 | print 'Wii Remote connected...\n' 42 | print 'Press some buttons!\n' 43 | print 'Press PLUS and MINUS together to disconnect and quit.\n' 44 | 45 | wii.rpt_mode = cwiid.RPT_BTN 46 | 47 | while True: 48 | 49 | buttons = wii.state['buttons'] 50 | 51 | # If Plus and Minus buttons pressed 52 | # together then rumble and quit. 53 | if (buttons - cwiid.BTN_PLUS - cwiid.BTN_MINUS == 0): 54 | print '\nClosing connection ...' 55 | wii.rumble = 1 56 | time.sleep(1) 57 | wii.rumble = 0 58 | exit(wii) 59 | 60 | # Check if other buttons are pressed by 61 | # doing a bitwise AND of the buttons number 62 | # and the predefined constant for that button. 63 | if (buttons & cwiid.BTN_LEFT): 64 | print 'Left pressed' 65 | time.sleep(button_delay) 66 | #io.output(2, True) 67 | 68 | if(buttons & cwiid.BTN_RIGHT): 69 | print 'Right pressed' 70 | time.sleep(button_delay) 71 | #io.output(3, True) 72 | 73 | if (buttons & cwiid.BTN_UP): 74 | print 'Up pressed' 75 | time.sleep(button_delay) 76 | #io.output(4, True) 77 | 78 | if (buttons & cwiid.BTN_DOWN): 79 | print 'Down pressed' 80 | time.sleep(button_delay) 81 | #io.output(17, True) 82 | 83 | if (buttons & cwiid.BTN_1): 84 | print 'Button 1 pressed' 85 | time.sleep(button_delay) 86 | 87 | if (buttons & cwiid.BTN_2): 88 | print 'Button 2 pressed' 89 | time.sleep(button_delay) 90 | 91 | if (buttons & cwiid.BTN_A): 92 | print 'Button A pressed' 93 | time.sleep(button_delay) 94 | #for i in pins: 95 | #io.output(i, False) 96 | 97 | if (buttons & cwiid.BTN_B): 98 | print 'Button B pressed' 99 | time.sleep(button_delay) 100 | 101 | if (buttons & cwiid.BTN_HOME): 102 | print 'Home Button pressed' 103 | time.sleep(button_delay) 104 | 105 | if (buttons & cwiid.BTN_MINUS): 106 | print 'Minus Button pressed' 107 | time.sleep(button_delay) 108 | 109 | if (buttons & cwiid.BTN_PLUS): 110 | print 'Plus Button pressed' 111 | time.sleep(button_delay) 112 | -------------------------------------------------------------------------------- /wii_controller.py: -------------------------------------------------------------------------------- 1 | # CamJam EduKit 3 - Robotics 2 | # Wii controller remote control script 3 | 4 | import RPi.GPIO as GPIO # Import the GPIO Library 5 | import time # Import the Time library 6 | import os 7 | 8 | # Set the GPIO modes 9 | GPIO.setmode(GPIO.BCM) 10 | GPIO.setwarnings(False) 11 | 12 | # Set variables for the GPIO motor pins 13 | pinMotorAForwards = 10 14 | pinMotorABackwards = 9 15 | pinMotorBForwards = 8 16 | pinMotorBBackwards = 7 17 | 18 | # Set the GPIO Pin mode 19 | GPIO.setup(pinMotorAForwards, GPIO.OUT) 20 | GPIO.setup(pinMotorABackwards, GPIO.OUT) 21 | GPIO.setup(pinMotorBForwards, GPIO.OUT) 22 | GPIO.setup(pinMotorBBackwards, GPIO.OUT) 23 | 24 | # Turn all motors off 25 | def StopMotors(): 26 | GPIO.output(pinMotorAForwards, 0) 27 | GPIO.output(pinMotorABackwards, 0) 28 | GPIO.output(pinMotorBForwards, 0) 29 | GPIO.output(pinMotorBBackwards, 0) 30 | 31 | # Turn both motors forwards 32 | def Forwards(): 33 | GPIO.output(pinMotorAForwards, 1) 34 | GPIO.output(pinMotorABackwards, 0) 35 | GPIO.output(pinMotorBForwards, 1) 36 | GPIO.output(pinMotorBBackwards, 0) 37 | 38 | # Turn both motors backwards 39 | def Backwards(): 40 | GPIO.output(pinMotorAForwards, 0) 41 | GPIO.output(pinMotorABackwards, 1) 42 | GPIO.output(pinMotorBForwards, 0) 43 | GPIO.output(pinMotorBBackwards, 1) 44 | 45 | def Left(): 46 | GPIO.output(pinMotorAForwards, 1) 47 | GPIO.output(pinMotorABackwards, 0) 48 | GPIO.output(pinMotorBForwards, 0) 49 | GPIO.output(pinMotorBBackwards, 1) 50 | 51 | def Right(): 52 | GPIO.output(pinMotorAForwards, 0) 53 | GPIO.output(pinMotorABackwards, 1) 54 | GPIO.output(pinMotorBForwards, 1) 55 | GPIO.output(pinMotorBBackwards, 0) 56 | 57 | StopMotors() 58 | 59 | # Credit for this part must go to: 60 | # Author : Matt Hawkins (adapted by Michael Horne) 61 | # http://www.raspberrypi-spy.co.uk/?p=1101 62 | # ----------------------- 63 | # Import required Python libraries 64 | # ----------------------- 65 | import cwiid 66 | 67 | PIN_LED = 25 68 | GPIO.setup(PIN_LED, GPIO.OUT) 69 | GPIO.output(PIN_LED, 0) 70 | 71 | button_delay = 0.1 72 | 73 | print 'Press 1 + 2 on your Wii Remote now ...' 74 | GPIO.output(PIN_LED, 1) 75 | time.sleep(1) 76 | 77 | # Connect to the Wii Remote. If it times out 78 | # then quit. 79 | try: 80 | wii=cwiid.Wiimote() 81 | GPIO.output(PIN_LED, 0) 82 | 83 | except RuntimeError: 84 | print "Error opening wiimote connection" 85 | GPIO.output(PIN_LED, 0) 86 | # Uncomment this line to shutdown the Pi if pairing fails 87 | #os.system("sudo halt") 88 | quit() 89 | 90 | print 'Wii Remote connected...\n' 91 | print 'Press some buttons!\n' 92 | print 'Press PLUS and MINUS together to disconnect and quit.\n' 93 | 94 | for x in range(0,3): 95 | GPIO.output(PIN_LED, 1) 96 | time.sleep(0.25) 97 | GPIO.output(PIN_LED, 0) 98 | time.sleep(0.25) 99 | 100 | wii.rpt_mode = cwiid.RPT_BTN 101 | 102 | while True: 103 | 104 | buttons = wii.state['buttons'] 105 | 106 | # If Plus and Minus buttons pressed 107 | # together then rumble and quit. 108 | if (buttons - cwiid.BTN_PLUS - cwiid.BTN_MINUS == 0): 109 | print '\nClosing connection ...' 110 | wii.rumble = 1 111 | GPIO.output(PIN_LED, 1) 112 | time.sleep(1) 113 | wii.rumble = 0 114 | GPIO.output(PIN_LED, 0) 115 | os.system("sudo halt") 116 | exit(wii) 117 | 118 | # Check if other buttons are pressed by 119 | # doing a bitwise AND of the buttons number 120 | # and the predefined constant for that button. 121 | if (buttons & cwiid.BTN_LEFT): 122 | print 'Left pressed' 123 | Left() 124 | time.sleep(button_delay) 125 | 126 | elif(buttons & cwiid.BTN_RIGHT): 127 | print 'Right pressed' 128 | Right() 129 | time.sleep(button_delay) 130 | 131 | elif (buttons & cwiid.BTN_UP): 132 | print 'Up pressed' 133 | Forwards() 134 | time.sleep(button_delay) 135 | 136 | elif (buttons & cwiid.BTN_DOWN): 137 | print 'Down pressed' 138 | Backwards() 139 | time.sleep(button_delay) 140 | 141 | else: 142 | StopMotors() 143 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # EduKit3-Bluetooth 2 | 3 | ## Credits 4 | A big thank you to Matt Hawkins (http://www.raspberrypi-spy.co.uk/) and the Raspberry Pi Foundation for the Bluetooth and CWiid instructions. 5 | 6 | ## Notes 7 | Please consider these instructions a BETA until they are absorbed and reformatted into the EduKit 3 worksheets. 8 | 9 | Please note: these instructions will only work for 1st edition Wii controllers 10 | i.e. those without Wii motionplus. This is a limitation of the CWiid library. 11 | 12 | ## You will need: 13 | * A Raspberry Pi connected up to the EduKit motor controller board with all the 14 | motors and battery pack connected up. It's assumed that you've worked through 15 | at least as far as worksheet 4. 16 | * A portable power source for the Pi, such as a phone charger battery. 17 | * A USB cable to attach the power source to the Raspberry Pi 18 | * A Wiimote (first edition, without motionplus) 19 | * Bluetooth capability. So, one of: 20 | * Raspberry Pi 3 21 | * Raspberry Pi Zero + USB adapter + bluetooth dongle 22 | * Raspberry Pi (other model) + bluetooth dongle 23 | 24 | ## Software installation 25 | Run the following command to install the bluetooth service: 26 | 27 | `sudo apt-get install --no-install-recommends bluetooth` 28 | 29 | Once finished, type: 30 | 31 | `sudo service bluetooth status` 32 | 33 | You should find that the service is Active 34 | 35 | Type the following to scan for bluetooth devices: 36 | 37 | `hcitool scan` 38 | 39 | ## Finding and pairing 40 | Now to find the Wii controller. 41 | Make sure your Wii controller has batteries in it :-) 42 | 43 | Type: 44 | 45 | `hcitool scan` 46 | 47 | and press the 1 and 2 buttons on your Wiimote at the same time. 48 | The blue LEDs on the Wiimote will flash and your controller should be found. 49 | It will have the word "Nintendo" in it if you are using an official controller. 50 | 51 | ## Python library 52 | 53 | The next thing to do is to make sure that Python can connect to the controller. 54 | For this, we use a library called `cwiid`. Install it using the following: 55 | 56 | `sudo apt-get install python-cwiid` 57 | 58 | Now, get the code from this repository: 59 | 60 | `git clone https://github.com/recantha/EduKit3-bluetooth` 61 | 62 | Change folder: 63 | 64 | `cd EduKit3-bluetooth` 65 | 66 | Run the test 67 | 68 | `python wii_remote_test.py` 69 | 70 | When the script tells you to, press the 1+2 buttons on your Wii remote. 71 | It should connect properly. If it doesn't work, try a bit closer or a bit 72 | further away. 73 | 74 | Now for the exciting part. Using the Wii controller to control the robot. 75 | There are two parts to this: With a monitor and headless. 76 | 77 | First of all, turn your motor battery pack on! 78 | 79 | Now, run the controller script: 80 | 81 | `python wii_controller.py` 82 | 83 | Follow the instructions to pair the Wii controller again. 84 | Once it has paired, you should find that your robot responds to the arrow 85 | buttons. To quit, press PLUS and MINUS together. 86 | 87 | Now, to run headless. For this, you will need an LED. Any one will do. 88 | We're going to do something that's not recommended - using an LED without 89 | a current-resisting resistor. I've tried this myself and it's not caused 90 | any issues. Take an LED and insert the short leg into the driver board's 91 | female header marked GND. Insert the longer, positive, leg into the pin marked 92 | 25. 93 | 94 | Run the script again: 95 | 96 | `python wii_controller.py` 97 | 98 | This time, you should notice the LED lighting up when pairing begins 99 | and flashing when the controller is connected. Useful, especially 100 | when you have no idea what's happening 'on screen' - i.e. when you're 101 | running 'headless'. 102 | 103 | Now, we need to make the script work on bootup. There are a number of 104 | ways to do this but I've chosen this way because it's slightly easier. 105 | 106 | First of all, make sure your Pi boots to desktop: 107 | Go to Menu->Preferences->Raspberry Pi configuration and make sure 108 | that Boot is set to 'To Desktop' and that Auto Login is set to 'Login as user pi' 109 | Click OK to confirm. 110 | 111 | Now, open up a terminal and type the following: 112 | 113 | `sudo nano ~/.config/lxsession/LXDE-pi/autostart` 114 | 115 | At the bottom of the file, add the following: 116 | 117 | `@/usr/bin/python /home/pi/EduKit3-bluetooth/wii_controller.py` 118 | 119 | Reboot your Pi: 120 | `sudo reboot` 121 | 122 | Wait for the Pi to come back up - the LED should light up to tell you to 123 | press 1+2 on the Wii controller. It should flash when it's correctly paired. 124 | If the LED goes off without flashing, you need to reboot the Pi. 125 | 126 | ## Doing it for real 127 | 128 | Now shutdown your Pi by typing: 129 | 130 | `sudo halt` 131 | 132 | Disconnect your keyboard, mouse, power cable so that your Pi is untethered 133 | and then attach your portable Pi power source. 134 | 135 | The Pi should boot up, the LED should light up. As before, press 1+2 on your 136 | Wiimote. The LED should flash to let you know you've done it. 137 | 138 | Away you go! 139 | 140 | ## Contacting me 141 | You can either email me at mike (at) recantha.co.uk or via my blog: http://www.recantha.co.uk/blog/?page_id=610 142 | --------------------------------------------------------------------------------