Multiwii + RasPi + PS3 controller = FUN!

This forum is dedicated to all issues and questions related to your individual setups and configurations
Post Reply
hakucookie
Posts: 3
Joined: Sun Oct 19, 2014 12:01 pm

Multiwii + RasPi + PS3 controller = FUN!

Post by hakucookie »

So I spent a couple of hours typing out a beautiful guide on how to get them working together and then it went and lost it. Grrrrrrrrrrrrrr!

Here's how I got my multiwii and raspi working together to fly!

##################################
Disclaimer: If you break something by copying me, Ooops! Not my fault :P
#################################


We need a multiwii and a raspberry pi (version doesn't matter)

We also need some additional software on the raspi soooooooooooooooo

Code: Select all

## as root ##
root@multi-pi:~# apt-get install joystick
root@multi-pi:~# wget http://github.com/richardghirst/PiBits/archive/master.zip && unzip master.zip && cd PiBits-master/ServoBlaster/user/ && make install


now open a file and call it quad.py (or something easy to figure out)

Code: Select all

root@multi-pi:~# nano quad.py


and paste the following into it

Code: Select all

#!/usr/bin/python
#Python cares about whitespace so don't jiggle anything around unless I've broken it here
#94% of the code here was written by my friend Jnet, I just kept poking him with a stick until it worked :D
#and then I changed all the names and added the comments so hopefully anyone can understand how it works

import pygame                                                             #This is the library for joystick inputs
import numpy                                                              #This is the library that does range mapping (convert a number from one range into another)

pygame.init()
pygame.joystick.init()

#Open Game Pad
gamepad = [pygame.joystick.Joystick(x) for x in range(pygame.joystick.get_count())]
gamepad[0].init()

done = False
while done == False:
        for event in pygame.event.get():
                if event.type == pygame.JOYAXISMOTION:
                        if event.axis == 0:                                  #Event.axis == 0 refers to the first joysticks left-right movement
                                sblast = open('/dev/servoblaster', 'w')                                  #This opens the "radio" ready to send a signal
                                roll = numpy.interp(event.value,[-1,1],[100,200])                #This converts the left sticks left to right position to a "radio" value
                                print("Roll Postion =", roll)                                                  #This isn't necessary but is useful for debugging, if you have a screen
                                sblast.write("1=" + str(int(roll)) + '\n')                                  # "1=" refers to servoblasters 2nd output on pin11 of the raspberry GPIO
                                sblast.close()                                                                      #This actually transmits it, don't ask me why but it only tx's on close
                        if event.axis == 1:                                   #Event.axis == 1 refers to the first joysticks up-down movement
                                sblast = open('/dev/servoblaster', 'w')
                                pitch = numpy.interp(event.value,[-1,1],[100,200])
                                print("Pitch Postion =", pitch)
                                sblast.write("2=" + str(int(pitch)) + '\n')                                # "2=" refers to servoblasters 3rd output on pin12 of the raspberry GPIO
                                sblast.close()
                        if event.axis == 2:                                    #Event.axis == 2 refers to the 2nd joysticks left-right movement
                                sblast = open('/dev/servoblaster', 'w')
                                yaw = numpy.interp(event.value,[-1,1],[101,199])
                                print("Yaw Postion =", yaw)
                                sblast.write("3=" + str(int(yaw)) + '\n')                                  # "3=" refers to servoblasters 4th output on pin13 of the raspberry GPIO
                                sblast.close()
                        if event.axis == 3:                                    #Event.axis == 3 refers to the 2nd joysticks up-down movement
                                sblast = open('/dev/servoblaster', 'w')
                                throttle = numpy.interp(event.value,[-1,1],[90,180])
                                print("Throttle Postion =", throttle)
                                sblast.write("0=" + str(int(throttle)) + '\n')                            # "0=" refers to servoblasters 1st output on pin7 of the raspberry GPIO
                                sblast.close()
                if event.type == pygame.JOYBUTTONDOWN:         # If ANY BUTTON is pressed then the throttle is set to MIN and it should come down level-ish
                        sblast = open('/dev/servoblaster', 'w')
                        print("ABORT!!!!!")
                        sblast.write("0=100\n1=150\n2=150\n3=150" + '\n')            # If you have a heavy quadcopter change 0=100 to 0=110 or 120 130 or 140 etc so it doesn't crash so hard
                        sblast.close()
                        done = True

Save and close, don't close and sa-oh shi!

Now add 2 lines to /etc/rc1.d/K01servoblaster so it looks like this

Code: Select all

case "$1" in
  start)
        /usr/local/sbin/servod $OPTS >/dev/null
        /bin/date >>/root/flight-recorder.txt                                                   ####### ADD THIS LINE
        /usr/bin/python /root/quad.py >>/root/flight-recorder.txt &                ####### AND THIS LINE
        ;;


Save and close

/root/flight-recorder.txt will basically be your way of looking back on things to see what might have gone wrong and where but it only records commands sent by the raspi to the Multiwii, not what the Multiwii received.

Things to note:
I am using a usb "PS2"(knockoff) joypad but I plan on implementing either bluetooth PS3(genuine) joypad control or wifi control via a laptop.
Pi Cam recording for flights (easy to do, just need to add raspivid -o filename.mp4 somewhere and make a little frame to mount the camera module)
The joypad needs to be detected before the script tries to run otherwise the script will fail (because it needs a joypad to run)

Obligatory pics of my RasPi + Multiwii breakout board
Idle ////////////// Armed
ImageImage

Top of breakout board + foam ////////////// Bottom of breakout board
ImageImage

copterrichie
Posts: 2261
Joined: Sat Feb 19, 2011 8:30 pm

Re: Multiwii + RasPi + PS3 controller = FUN!

Post by copterrichie »

Nice Job.

hakucookie
Posts: 3
Joined: Sun Oct 19, 2014 12:01 pm

Re: Multiwii + RasPi + PS3 controller = FUN!

Post by hakucookie »

Thanks, the most annoying thing about this whole project was that nobody else had documented how they did it before, like that one with the cardboard frame and the raspi with the webcam. So I wanted to get this mini guide out there for anyone to see if it could be helpful to anyone. Another thing I did was disabled the raspi serial terminal interface (google it) so I could connect the RX and TX pins to the UART TX and RX pins, it worked with a mess of wires but I haven't got it working again on the board I made, hopefully just a config issue though.

Goal of the project - control a quadcopter without having to buy an expensive 5+ channel transmitter and equally expensive receiver. For a £30 Pi and a £7 controller I don't think I did too badly lol

copterrichie
Posts: 2261
Joined: Sat Feb 19, 2011 8:30 pm

Re: Multiwii + RasPi + PS3 controller = FUN!

Post by copterrichie »

The issue of connecting the Raspi serial to an Arduino, the Pi is 3.3 volts and the Arduino is 5 volts. There are a number of ways of doing that, I opted with a voltage divider on the Raspi RX and connected the TX directly. the 3.3 volts is high enough for the Arduino to read however, it would be best if a logic lever is used.

Note: it may be possible to use a 2.7 volt Zener diode between the Arduino TX and the Raspi RX.

Hoppsan_84
Posts: 51
Joined: Thu Jul 10, 2014 2:13 pm

Re: Multiwii + RasPi + PS3 controller = FUN!

Post by Hoppsan_84 »

So for now you fly whit the cable hanging between the copter and the joypad?

copterrichie
Posts: 2261
Joined: Sat Feb 19, 2011 8:30 pm

Re: Multiwii + RasPi + PS3 controller = FUN!

Post by copterrichie »

No, I use Wifi however, I have not completed the system. Been on the back shelf for awhile. Was just looking at it today before reading your post. :)

Will have to get back on it now. ;)

hakucookie
Posts: 3
Joined: Sun Oct 19, 2014 12:01 pm

Re: Multiwii + RasPi + PS3 controller = FUN!

Post by hakucookie »

Wait ... they're only 3.3v? Oops! explains why it wasn't working then lol though oddly I did get it working once but haven't tried a lot since, I'm more annoyed by the slow post and final parts not arriving 2 weeks ago grrr, oh well, it all adds to the suspense!

Post Reply