Copy the sample code to a folder on the Raspberry Pi.
Open terminal, and then go to the folder you copied the sample.
Run the sample with the following command:
python3 Controller_PWM.py
Sample Code
Controller_PWM.py
#!/usr/bin/env python3#This sample demonstrates using PWM outputs on controller port.#Install RC Driver HAT library with "pip3 install turta-rcdriverhat"from time import sleepfrom turta_rcdriverhat import Turta_RCDriver#Initializerc = Turta_RCDriver.RCDriver()try:#Activate fan at 50% speed rc.set_fan(50)whileTrue:#Option one: Set all the pulses in one shot#-128 is 1000us, 0 is 1500us, 128 is 2000us#Channels: Steering, Throttle, Aux 3, Aux 4print("PWMs are S: 32, T: 64, 3: 96, 4: 128") rc.set_pwms([32, 64, 96, 128])#Waitsleep(5.0)#Option two: Set the pulses one by one#-128 is 1000us, 0 is 1500us, 128 is 2000usprint("PWMs are S: -32, T: -64, 3: -96, 4: -128") rc.set_pwm(Turta_RCDriver.PWM_OUT.STEERING, -32) rc.set_pwm(Turta_RCDriver.PWM_OUT.THROTTLE, -64) rc.set_pwm(Turta_RCDriver.PWM_OUT.AUX_3, -96) rc.set_pwm(Turta_RCDriver.PWM_OUT.AUX_4, -128)#Waitsleep(5.0)#Demo: Changing pulsesprint("Steering servo ch: 1100 to 1900us")for p inrange(-100, 100, 2): rc.set_pwm(Turta_RCDriver.PWM_OUT.STEERING, p)sleep(0.05)print("Steering servo ch: 1900 to 1100us")for p inrange(100, -100, -2): rc.set_pwm(Turta_RCDriver.PWM_OUT.STEERING, p)sleep(0.05)#Waitsleep(1.0)print("Turn off pulse generation") rc.set_pwms_off()#Waitsleep(5.0)#Exit on CTRL+CexceptKeyboardInterrupt:print('Bye.')
Result
When you run the sample, it generates different pulses on control port. The example output should be like this:
PWMs are S: 32, T: 64, 3: 96, 4: 128
PWMs are S: -32, T: -64, 3: -96, 4: -128
Steering servo ch: 1100 to 1900us
Steering servo ch: 1900 to 1100us
Turn off pulse generation