Setting ServoStik using python and GPIO pins
corminos last edited by mitu
Raspberry Pi 3B+
I was following a tutorial from this page: https://andrewdupont.net/2017/07/10/nostalgia-tron-part-8-over-engineered-joystick-rotation/
I was trying to do the same thing where I would control a ServoStik from a python script using the runcommand-onstart.sh hook.
I had to change the script a little bit, but I'm confident that the code is at least running the functions.
I've tried running my python script with "sudo", and my print statements tell me it should be setting the pins properly, but it still doesn't control my Servostik.
I've tested my cable by splicing the cut end of the USB cable back together and using the Windows utility. That would rule out the motor, control board and cable.
I tried hooking the positive and negative power to the 5v and ground GPIO pins. I've also tried connecting the positive and negative power to the cut end of the USB cable and plugging it into a USB port.
I'm at a loss for what I can try next.
Here is the python code:
#!/usr/bin/env python3 """ A script for setting the ServoStik to 4-way mode or 8-way mode. """ usage = """ Usage: set-joystick [4|8|0] Takes a single argument which should be either "4" (four-way), "8" (eight-way), or "0" (which means "I have no clue what to do and am willing to leave it up to this script to decide.") """ import sys #from RPi import GPIO import RPi.GPIO as GPIO GPIO.setmode(GPIO.BCM) MODE_8 = 6 # white MODE_4 = 5 # green GPIO.setup(MODE_4, GPIO.OUT) GPIO.setup(MODE_8, GPIO.OUT) def set_joystick_mode(mode): target_pin = MODE_8 other_pin = MODE_4 if mode == 4: target_pin = MODE_4 other_pin = MODE_8 print("set high",target_pin) GPIO.output(other_pin, GPIO.LOW) GPIO.output(target_pin, GPIO.HIGH) mode = sys.argv if mode not in ("0", "4", "8", ""): print("Invalid argument!", file=sys.stderr) print(usage) GPIO.cleanup() sys.exit(1) if mode == "0" or mode == "": #Zero equals a shrug. Put the joystick in 8-way mode. set_joystick_mode(8) elif mode == "4": set_joystick_mode(4) elif mode == "8": set_joystick_mode(8) GPIO.cleanup() sys.exit()
(Looking at the preview, I'm not sure if the spaces in the python code are going to come through properly)
Use code blocks (
```) to surround your code, so it's properly indendet and highlighted. I've edited the post and added them.
According to the RPI GPIO board numbering, Pins 5 and 6 do not correspond to what's in the diagram referenced in the article (which shows pins 8 and 10 connected). Pin 6 is ground, so I don't think it needs to be pulled high/low.
How did you wire the Servo to the GPIO pins ?
corminos last edited by
I did notice that too. Using the card that came with my raspberry pi, I connected the white and green wires to the GPIO pins 6 and 5, which are pin numbers 31 and 29, respectively.
I sent an email to Ultimarc and they said that the ServoStik could not be controlled through GPIO pins, but only through control transfers via a USB protocol. This sounds a lot more complex. I'm really hoping that there is another solution.
I'm really hoping that there is another solution.
What's the difference between GPIO and and USB connection ? It seems easier to me to conenct via USB. If this is the supported method, why aren't you using it ?
corminos last edited by
I see my problem now. When I first read the instructions on the link, I did not understand what he meant by "switching to hardware mode". At the time I did not have the joysticks and did not understand what that meant. Now I do.
I'm going to do some more reading and then give it a try. Just a little nervous because this is a one-way ticket and I really don't feel like shelling out more for another control board if I screw it up.
Thanks to those who read and responded. I love how active this forum is.