Issue with coding and servo kit code

Hello,

I recently started programming the gopigo to be controlled with my keyboard and it worked great however there were some slight issues. I was using a video to help me regarding the servo kit from sentdex. Now I know he has the outdated version of the module so I just adapted his code into the newer one. However I cant seem to figure out this line of code to control the servo kit using the number keys on my keyboard. This is the code:

servo_range = [2,3,4,5,6,7,8]
def key_input(event):

    key_press = event.keysym.lower()
    print(key_press)

    if key_press == 'w':
        gpg.forward()
    elif key_press == 's':
        gpg.backward()
    elif key_press == 'a':
        gpg.left()
    elif key_press == 'd':
        gpg.right()
    elif key_press == 'q':
        gpg.left_rot()
    elif key_press == 'e':
        gpg.right_rot()
    elif key_press == 'space':
        gpg.stop()
    elif key_press.isdigit():
        if int(key_press) in servo_range:
            enable_servo()
            servo(int(key_press)*14)
            disable_servo()

so Regarding the servo kit, I get an error stating that global name ‘enable_servo’ is not defined. Also, Regarding the gpg.right_rot() and gpg.left_rot() the error is EasyGoPiGo3’ object has no attribute ‘left_rot’.
Does anybody know the correct attributes for these?

From your post, it looks like you’re trying to control your servo with the easygopigo3 library.

This test code should rotate your servo through the range from 10° to 170° in 10° steps:

#!/usr/bin/env python
from __future__ import print_function
from __future__ import division

import easygopigo3 as easy
import time

#initialize GoPiGo3
gpg = easy.EasyGoPiGo3()

#ititialize servo, port can be "SERVO1" or "SERVO2"
servo = gpg.init_servo(port = "SERVO1")

for i in range(10,180,10):
    servo.rotate_servo(i)
    time.sleep(.25)

So if you initialize your servo as above, you should be able to change you code to:

elif key_press.isdigit():
    if int(key_press) in servo_range:
        servo.rotate_servo(int(key_press)*14)

Not sure about gpg.left_rot() or gpg.right_rot()

As you wrote above, these functions are not part of the easygopigo3 library. What are you trying to accomplish?

Edit:
I looked at the sentdex tutorial you referenced and it looks like he’s using the older version of the GoPiGo. This tutorial is also a couple of years old and the libraries are constantly evolving.

Look in the folder:
/home/pi/Dexter/GoPiGo3/Software/Python

Examine these files:

gopigo3.py
easygopigo3.py
easysensors.py

They are pretty well commented and they hold all the functions for programming your GoPiGo3.

-Kevin

Thanks for the help. Cant believe it went right over my head!

Hello,
I came and tested what you did and the I wasnt getting any errors returned however when I pressed the numbers the servo kit did not move but then suddenly I pressed 8 then it moved, so I tried pressing again it didnt work. If you press it at the beginning it moves slightly.

please let me know If im missing something.

from future import print_function
from future import division

import easygopigo3 as easy
import time
import Tkinter as tk

#initialize GoPiGo3
gpg = easy.EasyGoPiGo3()
servo_range = [2,3,4,5,6,7,8]

#ititialize servo, port can be “SERVO1” or “SERVO2”
servo = gpg.init_servo(port = “SERVO1”)

def key_input(event):

key_press = event.keysym.lower()
print(key_press)

if key_press == 'w':
    gpg.forward()
elif key_press == 's':
    gpg.backward()
elif key_press == 'a':
    gpg.left()
elif key_press == 'd':
    gpg.right()
elif key_press == 'q':
    gpg.left_rot()
elif key_press == 'e':
    gpg.right_rot()
elif key_press == 'space':
    gpg.stop()
elif key_press == 'z':
    increase_speed()
elif key_press == 'x':
    decrease_speed() 
elif key_press.isdigit():
    if int(key_press) in servo_range:
        servo.rotate_servo(int(key_press)*14)
        time.sleep(1)
command = tk.Tk()
command.bind_all(’’, key_input)
command.mainloop()

Edit:
Might be a hardware issue, the servo kit extremely overheated for some reason.

I tried your code. Could not get it to work until I changed line 43 from this:

command.bind_all('', key_input)

To this:

command.bind_all('<Key>', key_input)

Full source code here:

#!/usr/bin/env python
from __future__ import print_function
from __future__ import division

import easygopigo3 as easy
import time
import tkinter as tk

#initialize GoPiGo3
gpg = easy.EasyGoPiGo3()
servo_range = [2,3,4,5,6,7,8]

#ititialize servo, port can be SERVO1 or SERVO2
servo = gpg.init_servo(port = 'SERVO1')

def key_input(event):

    key_press = event.keysym.lower()
    print(key_press)

    if key_press == 'w':
        gpg.forward()
    elif key_press == 's':
        gpg.backward()
    elif key_press == 'a':
        gpg.left()
    elif key_press == 'd':
        gpg.right()
    elif key_press == 'q':
        gpg.left_rot()
    elif key_press == 'e':
        gpg.right_rot()
    elif key_press == 'space':
        gpg.stop()
    elif key_press == 'z':
        increase_speed()
    elif key_press == 'x':
        decrease_speed()
    elif key_press.isdigit():
        if int(key_press) in servo_range:
            servo.rotate_servo(int(key_press)*14)
            time.sleep(1)
command = tk.Tk()
command.bind_all('<Key>', key_input)
command.mainloop()

The code for the servo works now, but these functions are not defined:

  • gpg.left_rot()
  • gpg.right_rot()
  • increase_speed()
  • decrease_speed()

You’ll need to implement these functions, or delete the code that is calling them.

-Kevin