-1

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.

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Lord.V
  • 1
  • 1
  • Have you seen this solution SO already ? [link](https://stackoverflow.com/questions/15855168/create-a-raw-input-with-commands-inside-a-python-script) also.. there are a few of these questions already answered asking for similar things and people seem to mention the python [cmd](https://docs.python.org/2/library/cmd.html). I’ve never used it before but it might be what your looking for. – gyx-hh Mar 18 '18 at 22:35
  • Just be careful. If a pressing `w` moves 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. –  Mar 21 '18 at 13:47

2 Answers2

0

1)you can first of all make a infinite while loop.

2) after take input via row input

3) then after apply condition for which keyword is found then which function is called

4) now call the function if condition is true.

Devdatt
  • 82
  • 5
0

I'm working on my project using the same servo HAT, and have been looking for a similar solution. So far, my best results have been to use pygame's KEYDOWN event. https://www.pygame.org/news