Note
Hello, welcome to the SunFounder Raspberry Pi & Arduino & ESP32 Enthusiasts Community on Facebook! Dive deeper into Raspberry Pi, Arduino, and ESP32 with fellow enthusiasts.
Why Join?
Expert Support: Solve post-sale issues and technical challenges with help from our community and team.
Learn & Share: Exchange tips and tutorials to enhance your skills.
Exclusive Previews: Get early access to new product announcements and sneak peeks.
Special Discounts: Enjoy exclusive discounts on our newest products.
Festive Promotions and Giveaways: Take part in giveaways and holiday promotions.
👉 Ready to explore and create with us? Click [here] and join today!
Keyboard Control¶
In this project, we will use w, s, a, d, i, k, j and l on the keyboard to control the PiArm.
Shovel Bucket - Keyboard Coboardntrol¶
Run the code
cd /home/pi/piarm/examples
sudo python3 keyboard_control1.py
After running the code, follow the prompts and press the keys on the keyboard to control the PiArm’s arm and Shovel Bucket.
But you need to assemble Shovel Bucket on the PiArm first.
Note
To switch the keyboard to lowercase English input.
w,s,a,d,iandkare used to control the rotation of the arm.jandlare used to control the angle of the Shovel Bucket.
Code
from piarm import PiArm
from robot_hat import Pin,PWM,Servo,ADC
from time import time,sleep
from robot_hat.utils import reset_mcu
import sys
import tty
import termios
reset_mcu()
sleep(0.01)
arm = PiArm([1,2,3])
arm.bucket_init(PWM('P3'))
arm.set_offset([0,0,0])
controllable = 0
manual = '''
Press keys on keyboard
w: extend
s: retract
a: turn left
d: turn right
i: go up
k: go down
j: open
l: close
ESC: Quit
'''
def readchar():
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
try:
tty.setraw(sys.stdin.fileno())
ch = sys.stdin.read(1)
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
return ch
def control(key):
arm.speed = 100
flag = False
alpha,beta,gamma = arm.servo_positions
bucket = arm.component_staus
if key == 'w':
alpha += 3
flag = True
elif key == 's':
alpha -= 3
flag = True
if key == 'a':
gamma += 3
flag = True
elif key == 'd':
gamma -= 3
flag = True
if key == 'i':
beta += 3
flag = True
elif key == 'k':
beta -= 3
flag = True
if key == 'j':
bucket -= 1
flag = True
elif key == 'l':
bucket += 1
flag = True
if flag == True:
arm.set_angle([alpha,beta,gamma])
arm.set_bucket(bucket)
print('servo angles: %s , bucket angle: %s '%(arm.servo_positions,arm.component_staus))
if __name__ == "__main__":
print(manual)
while True:
key = readchar().lower()
control(key)
if key == chr(27):
break
How it works?
def readchar():
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
try:
tty.setraw(sys.stdin.fileno())
ch = sys.stdin.read(1)
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
return ch
This function references the standard input stream and returns the first character of the read data stream.
tty.setraw(sys.stdin.fileno)is to change the standard input stream to raw mode, i.e. all characters will not be escaped during transmission, including special characters.old_settings = termios.tcgetattr(fd)andtermios.tcsetattr(fd, termios.TCSADRAIN, old_settings)and acts as a backup and restore.
def control(key):
arm.speed = 100
flag = False
alpha,beta,gamma = arm.servo_positions
bucket = arm.component_staus
if key == 'w':
alpha += 3
flag = True
elif key == 's':
alpha -= 3
flag = True
if key == 'a':
gamma += 3
flag = True
elif key == 'd':
gamma -= 3
flag = True
if key == 'i':
beta += 3
flag = True
elif key == 'k':
beta -= 3
flag = True
if key == 'j':
bucket -= 1
flag = True
elif key == 'l':
bucket += 1
flag = True
if flag == True:
arm.set_angle([alpha,beta,gamma])
arm.set_bucket(bucket)
print('servo angles: %s , bucket angle: %s '%(arm.servo_positions,arm.component_staus))
In this code, the control() function is created to control the PiArm by reading the key values on the keyboard.
alpha,betaandgammarefer to the angles of the 3 servos on the arm respectively, refer to: Angle Mode.Press the
wkey on the keyboard, thealphaincreases and the Arm will extend forward.Press the
skey on the keyboard, thealphadecreases and the Arm will retract backward.Press the
akey on the keyboard, thegammaincreases and the Arm will turn left.Press the
dkey on the keyboard, thegammadecreases and the Arm will turn right.Press the
ikey on the keyboard, thebetaincreases and the Arm will raise up.Press the
kkey on the keyboard, thebetadecreases and the Arm will lower down.Finally, use the
kandlkeys to control the angle of the Shovel Bucket respectively.
while True:
key = readchar().lower()
control(key)
if key == chr(27):
break
Call readchar() in the main program to read the key value, then pass the read key value into the control() function so that PiArm will move according to the different keys.
key == chr(27) represents the key Esc key press.
Hanging Clip - Keyboard Control¶
Run the code
cd /home/pi/piarm/examples
sudo python3 keyboard_control2.py
After running the code, follow the prompts and press the keys on the keyboard to control the Arm and Hanging Clip of PiArm.
But you need to assemble Hanging Clip to PiArm first.
Note
To switch the keyboard to lowercase English input.
w,s,a,d,iandkare used to control the rotation of the arm.jandlare used to control the opening and closing of the Hanging Clip.
Code
from piarm import PiArm
from robot_hat import Pin,PWM,Servo,ADC
from time import time,sleep
from robot_hat.utils import reset_mcu
import sys
import tty
import termios
reset_mcu()
sleep(0.01)
arm = PiArm([1,2,3])
arm.hanging_clip_init(PWM('P3'))
arm.set_offset([0,0,0])
controllable = 0
manual = '''
Press keys on keyboard
w: extend
s: retract
a: turn left
d: turn right
i: go up
k: go down
j: open
l: close
ESC: Quit
'''
def readchar():
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
try:
tty.setraw(sys.stdin.fileno())
ch = sys.stdin.read(1)
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
return ch
def control(key):
arm.speed = 100
flag = False
alpha,beta,gamma = arm.servo_positions
clip = arm.component_staus
if key == 'w':
alpha += 3
flag = True
elif key == 's':
alpha -= 3
flag = True
if key == 'a':
gamma += 3
flag = True
elif key == 'd':
gamma -= 3
flag = True
if key == 'i':
beta += 3
flag = True
elif key == 'k':
beta -= 3
flag = True
if key == 'j':
clip -= 1
flag = True
elif key == 'l':
clip += 1
flag = True
if flag == True:
arm.set_angle([alpha,beta,gamma])
arm.set_hanging_clip(clip)
print('servo angles: %s , clip angle: %s '%(arm.servo_positions,arm.component_staus))
if __name__ == "__main__":
print(manual)
while True:
key = readchar().lower()
control(key)
if key == chr(27):
break
In this code, the control() function is created to control the PiArm by reading the key values on the keyboard.
alpha,betaandgammarefer to the angles of the 3 servos on the arm respectively, refer to: Angle Mode.Press the
wkey on the keyboard, thealphaincreases and the Arm will extend forward.Press the
skey on the keyboard, thealphadecreases and the Arm will retract backward.Press the
akey on the keyboard, thegammaincreases and the Arm will turn left.Press the
dkey on the keyboard, thegammadecreases and the Arm will turn right.Press the
ikey on the keyboard, thebetaincreases and the Arm will raise up.Press the
kkey on the keyboard, thebetadecreases and the Arm will lower down.Finally, use the
kandlkeys to control the opening and closing of the Hanging Clip respectively.
Electromagnet - Keyboard Control¶
Run the code
cd /home/pi/piarm/examples
sudo python3 keyboard_control1.py
After running the code, follow the prompts and press the keys on the keyboard to control the PiArm’s arms and Electromagnet.
But you need to assemble Electromagnet to PiArm first.
Note
To switch the keyboard to lowercase English input.
w,s,a,d,iandkare used to control the rotation of the arm.jandlare used to control the ON and OFF of the Electromagnet.
Code
from piarm import PiArm
from robot_hat import Pin,PWM,Servo,ADC
from time import time,sleep
from robot_hat.utils import reset_mcu
import sys
import tty
import termios
reset_mcu()
sleep(0.01)
arm = PiArm([1,2,3])
arm.electromagnet_init(PWM('P3'))
arm.set_offset([0,0,0])
controllable = 0
manual = '''
Press keys on keyboard
w: extend
s: retract
a: turn left
d: turn right
i: go up
k: go down
j: on
l: off
ESC: Quit
'''
def readchar():
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
try:
tty.setraw(sys.stdin.fileno())
ch = sys.stdin.read(1)
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
return ch
def control(key):
arm.speed = 100
flag = False
alpha,beta,gamma = arm.servo_positions
status = ""
if key == 'w':
alpha += 3
flag = True
elif key == 's':
alpha -= 3
flag = True
if key == 'a':
gamma += 3
flag = True
elif key == 'd':
gamma -= 3
flag = True
if key == 'i':
beta += 3
flag = True
elif key == 'k':
beta -= 3
flag = True
if key == 'j':
arm.set_electromagnet('on')
elif key == 'l':
arm.set_electromagnet('off')
if flag == True:
arm.set_angle([alpha,beta,gamma])
print('servo angles: %s , electromagnet status: %s '%(arm.servo_positions,status))
if __name__ == "__main__":
print(manual)
while True:
key = readchar().lower()
control(key)
if key == chr(27):
break
In this code, the control() function is created to control the PiArm by reading the key values on the keyboard.
alpha,betaandgammarefer to the angles of the 3 servos on the arm respectively, refer to: Angle Mode.Press the
wkey on the keyboard, thealphaincreases and the Arm will extend forward.Press the
skey on the keyboard, thealphadecreases and the Arm will retract backward.Press the
akey on the keyboard, thegammaincreases and the Arm will turn left.Press the
dkey on the keyboard, thegammadecreases and the Arm will turn right.Press the
ikey on the keyboard, thebetaincreases and the Arm will raise up.Press the
kkey on the keyboard, thebetadecreases and the Arm will lower down.Finally, use the
kandlkeys to control the ON and OFF of the Electromagnet respectively.