Currently building Petrol-powered RC car controlled by a raspberry pi and 16ch adafruit servo controller Pi hat. Pretty novice query from a beginner but how can simple Python commands be carried out by a single key press. E.g. Holding the "w" key on a keyboard to run "pwm.setPWM(0, 0, servoMax)". (In order for the servo to push the throttle to move the vehicle forward). What follows is the code currently used:
#!/usr/bin/python
from Adafruit_PWM_Servo_Driver import PWM
import time
pwm = PWM(0x40)
servoMin = 150
servoMax = 600
def setServoPulse(channel, pulse):
pulseLength = 1000000
pulseLength /= 60
print "%d us per period" % pulseLength
pulseLength /= 4096
print "%d us per bit" % pulseLength
pulse *= 1000
pulse /= pulseLength
pwm.setPWM(channel, 0, pulse)
pwm.setPWMFreq(60)
While (True):
pwm.setPWM(0, 0, servoMin) #throttle servo set to off position -should be default
pwm.setPWM(0, 0, servoMAX) #throttle servo set on -to be run by "W" key
pwm.setPWM(1, 0, servoMin) #steering servo left -by holding "A" key
pwm.setPWM(1, 0, servoMax) #steering servo right -by holding "D" key
I would assume the answer involves If and ElseIf commands, but I really would just like to run a program then input() keyboard presses to run the code.
wmoves the robot and releasing the key stops it, then if the robot gets out of range will keep moving forever. Trust me, you will need a more complex system where the robot pings the computer and activates a safe stop if no ping is received back.