├── README.md ├── install.sh ├── joystick.py └── joystick.service /README.md: -------------------------------------------------------------------------------- 1 | # PupperCommand 2 | ## Installation 3 | ```shell 4 | git clone https://github.com/stanfordroboticsclub/PupperCommand.git 5 | cd PupperCommand 6 | sudo bash install.sh 7 | ``` 8 | Then clone https://github.com/stanfordroboticsclub/PS4Joystick/ and follow the installation instructions in the README. 9 | 10 | ## Starting the joystick publisher 11 | 1. The ```install.sh``` script makes the Raspberry Pi automatically look to pair and connect to PS4 joysticks on boot. 12 | 2. So once the Raspberry Pi turns on, put the PS4 controller into pairing mode by holding the share and PS button at the same time. The light should start blinking in bursts of two. 13 | 3. By around 10 seconds, the joystick should have paired with the Raspberry Pi and the front light on the joystick will change to whatever color you specify in the ```joystick.py``` script. 14 | 15 | ## Debugging 16 | To see if the controller is publishing to the Rover topic use: 17 | ```shell 18 | rover peek 8830 19 | ``` 20 | 21 | You can also check the status of the system daemon (systemd) running the ```joystick.py``` script by doing 22 | ```shell 23 | sudo systemctl status joystick 24 | ``` 25 | If it shows that the service failed, you can try 26 | ```shell 27 | sudo systemctl stop joystick 28 | sudo systemctl start joystick 29 | ``` 30 | 31 | ## Notes 32 | If a packet is lost over the joystick connection, the PS4Joystick code will raise an exception and cause the program to exit. Systemd will then restart the ```joystick.py``` script, which means you will have to re-pair the joystick (hold share + ps4 button until double blinking). 33 | 34 | -------------------------------------------------------------------------------- /install.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | 3 | FOLDER=$(dirname $(realpath "$0")) 4 | cd $FOLDER 5 | 6 | sudo apt-get install -y libsdl-ttf2.0-0 7 | yes | sudo pip install ds4drv 8 | 9 | for file in *.service; do 10 | [ -f "$file" ] || break 11 | sudo ln -s $FOLDER/$file /lib/systemd/system/ 12 | done 13 | 14 | sudo systemctl daemon-reload 15 | sudo systemctl enable joystick 16 | sudo systemctl start joystick 17 | -------------------------------------------------------------------------------- /joystick.py: -------------------------------------------------------------------------------- 1 | from UDPComms import Publisher, Subscriber, timeout 2 | from PS4Joystick import Joystick 3 | 4 | import time 5 | 6 | ## you need to git clone the PS4Joystick repo and run `sudo bash install.sh` 7 | 8 | ## Configurable ## 9 | MESSAGE_RATE = 20 10 | PUPPER_COLOR = {"red":0, "blue":0, "green":255} 11 | 12 | joystick_pub = Publisher(8830) 13 | joystick_subcriber = Subscriber(8840, timeout=0.01) 14 | joystick = Joystick() 15 | joystick.led_color(**PUPPER_COLOR) 16 | 17 | while True: 18 | print("running") 19 | values = joystick.get_input() 20 | 21 | left_y = -values["left_analog_y"] 22 | right_y = -values["right_analog_y"] 23 | right_x = values["right_analog_x"] 24 | left_x = values["left_analog_x"] 25 | 26 | L2 = values["l2_analog"] 27 | R2 = values["r2_analog"] 28 | 29 | R1 = values["button_r1"] 30 | L1 = values["button_l1"] 31 | 32 | square = values["button_square"] 33 | x = values["button_cross"] 34 | circle = values["button_circle"] 35 | triangle = values["button_triangle"] 36 | 37 | dpadx = values["dpad_right"] - values["dpad_left"] 38 | dpady = values["dpad_up"] - values["dpad_down"] 39 | 40 | msg = { 41 | "ly": left_y, 42 | "lx": left_x, 43 | "rx": right_x, 44 | "ry": right_y, 45 | "L2": L2, 46 | "R2": R2, 47 | "R1": R1, 48 | "L1": L1, 49 | "dpady": dpady, 50 | "dpadx": dpadx, 51 | "x": x, 52 | "square": square, 53 | "circle": circle, 54 | "triangle": triangle, 55 | "message_rate": MESSAGE_RATE, 56 | } 57 | joystick_pub.send(msg) 58 | 59 | try: 60 | msg = joystick_subcriber.get() 61 | joystick.led_color(**msg["ps4_color"]) 62 | except timeout: 63 | pass 64 | 65 | time.sleep(1 / MESSAGE_RATE) 66 | -------------------------------------------------------------------------------- /joystick.service: -------------------------------------------------------------------------------- 1 | [Unit] 2 | Description=Pupper Joystick service 3 | 4 | [Service] 5 | ExecStart=/usr/bin/python3 /home/pi/PupperCommand/joystick.py 6 | Restart=always 7 | RestartSec=5 8 | KillSignal=2 9 | TimeoutStopSec=10 10 | RemainAfterExit=true 11 | 12 | [Install] 13 | WantedBy=multi-user.target 14 | --------------------------------------------------------------------------------