Universal Plus HATs RC receiver support Universal Plus Raspberry Pi HATs can read the signal from the RC receiver. Any port can be used. The read values can be used to control servos, drones, DACs or any supported by the HAT interfaces. The code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
def GetRatio(reading, margins): diff = margins[1] - margins[0] dx = (reading - margins[0]) / diff if dx < 0: dx = 0 if dx > 1: dx = 1 return dx servoPorts = [uzp0.J1, uzp0.J2] ledPort = [uzp0.PWM19] DACport = uzp0.DAC1 RCports = [10, 11, 12, 14] RC_ch = [[1140, 1917], [1140, 1917], [1090, 1958], [1068, 1906]] try: uzp0.GPIOInit([10,11,12,13]) uzp0.SMotorInit([14,15,16,17], 0) uzp0.SMotorStart(0) uzp0.SERVOInit(servoPorts) uzp0.PWMInit(ledPort) uzp0.PWMFrequencyDuty(ledPort, frequency = 1000, duty = 0) uzp0.PWMStart(ledPort) uzp0.DACInit(DACport) uzp0.DACStart(DACport) while True: RC_Readings = [uzp0.RCReceiverRead(10, True), uzp0.RCReceiverRead(11, True), uzp0.RCReceiverRead(12, True), uzp0.RCReceiverRead(13, True)] RC_Prop = [GetRatio(RC_Readings[0], RC_ch[0]), GetRatio(RC_Readings[1], RC_ch[1]), GetRatio(RC_Readings[2], RC_ch[2]), GetRatio(RC_Readings[3], RC_ch[3])] uzp0.PWMDuty(ledPort, RC_Prop[3] * 100) if RC_Prop[0] > 0.45 and RC_Prop[0] < 0.55: uzp0.SMotorStop(0) if RC_Prop[0] > 0.55: uzp0.SMotorStart(0) uzp0.SMotorSet(0, 10 - 8 * RC_Prop[0], 0xffff, overstep = 0, direction = 0) if RC_Prop[0] < 0.45: uzp0.SMotorStart(0) uzp0.SMotorSet(0, 10 - (8 - 8 * RC_Prop[0]), 0xffff, overstep = 0, direction = 1) uzp0.DACWrite(DACport, 4095 * RC_Prop[1]) position = (0.5 - RC_Prop[2]) * 200 uzp0.SERVOSetPos(servoPorts, position) except KeyboardInterrupt: uzp0.SMotorStop(0) |