RetroPie forum home
    • Recent
    • Tags
    • Popular
    • Home
    • Docs
    • Register
    • Login
    Please do not post a support request without first reading and following the advice in https://retropie.org.uk/forum/topic/3/read-this-first

    Setting ServoStik using python and GPIO pins

    Scheduled Pinned Locked Moved Help and Support
    gpiopythonservostik
    8 Posts 4 Posters 945 Views
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • C
      corminos
      last edited by mitu

      The details:
      Raspberry Pi 3B+
      RetroPie 4.5.1
      Ultimarc ServoStik

      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[1]
      
      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)
      Thanks

      1 Reply Last reply Reply Quote 0
      • mituM
        mitu Global Moderator
        last edited by

        Use code blocks (```) to surround your code, so it's properly indendet and highlighted. I've edited the post and added them.

        1 Reply Last reply Reply Quote 0
        • mituM
          mitu Global Moderator
          last edited by mitu

          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 ?

          1 Reply Last reply Reply Quote 0
          • C
            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.

            1 Reply Last reply Reply Quote 0
            • mituM
              mitu Global Moderator
              last edited by

              @corminos said in Setting ServoStik using python and GPIO pins:

              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 ?

              1 Reply Last reply Reply Quote 0
              • C
                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.

                I 1 Reply Last reply Reply Quote 0
                • I
                  imacmill @corminos
                  last edited by

                  @corminos said in Setting ServoStik using python and GPIO pins:

                  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.

                  Hey there. I'm just wondering if you had any luck with controlling a ServoStik with GPIO? Did you convert the board to hardware mode and have success?

                  Thanks.

                  flagrant99F 1 Reply Last reply Reply Quote 0
                  • flagrant99F
                    flagrant99 @imacmill
                    last edited by mitu

                    I just got it working with the following python script using rgbcommander's servostik linux command line. Hardware mode is not needed.

                    http://users.telenet.be/rgbcommander/

                    from gpiozero import Button
                    from subprocess import check_call
                    from signal import pause
                    
                    #Functions
                    
                    
                    
                    def onGpio3Open():
                    
                    #Arcade 1up switched to OFF check_call(['sudo', 'poweroff'])
                        print("gpio3Released")
                        check_call(['sudo', 'poweroff'])
                    
                    
                    def onGpio17Closed():
                        print("gpio17Pressed")
                        check_call(['sudo', './SetServoStik', '8'])
                    
                    
                    def onGpio27Closed():
                        print("gpio27Pressed")
                        check_call(['sudo', './SetServoStik', '4'])
                    
                    
                    
                    
                    #Main Execution Starts Here
                    gpio3Btn = Button(3)
                    gpio3Btn.when_released = onGpio3Open
                    
                    
                    gpio17Btn = Button(17)
                    gpio17Btn.when_pressed = onGpio17Closed
                    
                    
                    gpio27Btn = Button(27)
                    gpio27Btn.when_pressed = onGpio27Closed
                    
                    pause()
                    
                    1 Reply Last reply Reply Quote 0
                    • First post
                      Last post

                    Contributions to the project are always appreciated, so if you would like to support us with a donation you can do so here.

                    Hosting provided by Mythic-Beasts. See the Hosting Information page for more information.