Created
March 30, 2020 05:23
-
-
Save burrussmp/09e6d7d82cd144d8939675396c5fa753 to your computer and use it in GitHub Desktop.
A simple raspberry pi class for controlling the PWM GPIO pins of the Raspberry Pi 3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pigpio | |
import RPi.GPIO as GPIO | |
class PWM: | |
def __init__(self): | |
self.pi = pigpio.pi() | |
self.freq = 100 | |
self.steering = 0 | |
self.acceleration = 0 | |
self.gpioPinAcceleration = 18 | |
self.gpioPinSteering = 19 | |
# init hardware. Car should be faced forwards and not moving | |
self.pi.hardware_PWM(self.gpioPinAcceleration,self.freq,int(14.5*10000)) | |
self.pi.hardware_PWM(self.gpioPinSteering,self.freq,int(15*10000)) | |
# changeDutyCycle() | |
# Summary: Changes PWM duty cycles for steering and acceleration | |
# Parameter: data => tuple of doubles containing the duty cycles (%) for steering and acceleration | |
def changeDutyCycle(self,acc,steer): | |
if (self.steering != steer): | |
self.steering = steer | |
#print(steer) | |
self.pi.hardware_PWM(self.gpioPinSteering,self.freq,int(steer*10000)) | |
if (self.acceleration != acc): | |
self.acceleration = acc | |
#print(acc) | |
self.pi.hardware_PWM(self.gpioPinAcceleration,self.freq,int(acc*10000)) | |
# cleans GPIO | |
def __del__(self): | |
print('Cleaning up GPIO...') | |
GPIO.cleanup() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment