|
| 1 | +#***************************************************** |
| 2 | +#OW |
| 3 | +# STorM32 Python library to handle serial RC commands |
| 4 | +# http://www.olliw.eu/storm32bgc-wiki/Serial_Communication#Serial_Communication_-_RC_Commands |
| 5 | +# (c) olliw, www.olliw.eu, GPL3 |
| 6 | +# version 29. Nov. 2020 |
| 7 | +#***************************************************** |
| 8 | +# |
| 9 | +#***************************************************** |
| 10 | +# Example usage for CMD_SETANGLES: |
| 11 | +# import serial |
| 12 | +# from STorM32_lib import * |
| 13 | +# ser = serial.Serial("COM1", 115200) |
| 14 | +# cmd = cCMD_SETANGLES(ser) |
| 15 | +# cmd.send() |
| 16 | +#***************************************************** |
| 17 | + |
| 18 | +import struct |
| 19 | + |
| 20 | + |
| 21 | +#------------------------------------------------------ |
| 22 | +# Basic RCCMD Class |
| 23 | +#------------------------------------------------------ |
| 24 | + |
| 25 | +class cSTorM32RcCmd(): |
| 26 | + |
| 27 | + def __init__(self,ser=None,payload=None,noresponse=False): |
| 28 | + if noresponse: |
| 29 | + self.stx = b'\xF9' |
| 30 | + else: |
| 31 | + self.stx = b'\xFA' |
| 32 | + self.stx = b'\xF9' |
| 33 | + self.len = b'\x00' |
| 34 | + self.cmd = b'\x01' |
| 35 | + self.payload = b'' |
| 36 | + self.crc = b'\x33\x34' |
| 37 | + |
| 38 | + self.datastream = b'' |
| 39 | + self.ser = ser |
| 40 | + |
| 41 | + if payload != None: |
| 42 | + self.setPayload(payload) |
| 43 | + |
| 44 | + def enableResponse(self): |
| 45 | + self.stx = b'\xFA' |
| 46 | + return True |
| 47 | + |
| 48 | + def disableResponse(self): |
| 49 | + self.stx = b'\xF9' |
| 50 | + return True |
| 51 | + |
| 52 | + def setPayload(self,payload): |
| 53 | + self.payload = payload |
| 54 | + return True |
| 55 | + |
| 56 | + def finalize(self,noresponse=None): |
| 57 | + if noresponse == None: |
| 58 | + self.datastream = self.stx |
| 59 | + elif noresponse == True: |
| 60 | + self.datastream = b'\xF9' |
| 61 | + else: |
| 62 | + self.datastream = b'\xFA' |
| 63 | + self.datastream += self.len + self.cmd + self.payload + self.crc |
| 64 | + return True |
| 65 | + |
| 66 | + def getCmd(self): |
| 67 | + return self.datastream |
| 68 | + |
| 69 | + def send(self): |
| 70 | + if self.ser == None: |
| 71 | + return False |
| 72 | + if not self.finalize(): |
| 73 | + return False |
| 74 | + self.ser.write(self.datastream) |
| 75 | + return True |
| 76 | + |
| 77 | + |
| 78 | +#------------------------------------------------------ |
| 79 | +# RCCMD GetVersion #1 = 0x01 |
| 80 | +#------------------------------------------------------ |
| 81 | +# has a response |
| 82 | +# outgoing: 5 bytes = 434us @ 115200bps |
| 83 | + |
| 84 | +class cCMD_GETVERSION(cSTorM32RcCmd): |
| 85 | + |
| 86 | + def __init__(self,ser=None,payload=None,noresponse=False): |
| 87 | + super().__init__(ser,noresponse,payload) |
| 88 | + self.len = b'\x00' |
| 89 | + self.cmd = b'\x01' |
| 90 | + |
| 91 | + |
| 92 | +#------------------------------------------------------ |
| 93 | +# RCCMD GetVersionStr #2 = 0x02 |
| 94 | +#------------------------------------------------------ |
| 95 | +# has a response |
| 96 | +# outgoing: 5 bytes = 434us @ 115200bps |
| 97 | + |
| 98 | +class cCMD_GETVERSIONSTR(cSTorM32RcCmd): |
| 99 | + |
| 100 | + def __init__(self,ser=None,payload=None,noresponse=False): |
| 101 | + super().__init__(ser,noresponse,payload) |
| 102 | + self.len = b'\x00' |
| 103 | + self.cmd = b'\x02' |
| 104 | + |
| 105 | + |
| 106 | +#------------------------------------------------------ |
| 107 | +# RCCMD SetAngles #17 = 0x11 |
| 108 | +#------------------------------------------------------ |
| 109 | +# only outgoing, has no response (except of an ACK) |
| 110 | +# outgoing: 19 bytes = 1650us @ 115200bps |
| 111 | + |
| 112 | +class cCMD_SETANGLES (cSTorM32RcCmd): |
| 113 | + |
| 114 | + def __init__(self,ser=None,pitch_deg=0.0,roll_deg=0.0,yaw_deg=0.0,noresponse=True): |
| 115 | + super().__init__(ser,None,noresponse) |
| 116 | + self.len = b'\x0E' |
| 117 | + self.cmd = b'\x11' |
| 118 | + self.setPayload(pitch_deg,roll_deg,yaw_deg) |
| 119 | + |
| 120 | + def setPayload(self,pitch_deg,roll_deg,yaw_deg): |
| 121 | + self.pitch_deg = pitch_deg |
| 122 | + self.roll_deg = roll_deg |
| 123 | + self.yaw_deg = yaw_deg |
| 124 | + self.payload = struct.pack("fff", self.pitch_deg, self.roll_deg, self.yaw_deg) + b'\x00\x00' |
| 125 | + |
| 126 | + |
| 127 | +#------------------------------------------------------ |
| 128 | +# RCCMD SendCameraCommand #27 = 0x1B |
| 129 | +#------------------------------------------------------ |
| 130 | +# only outgoing, has no response (except of an ACK) |
| 131 | +# outgoing: 6 - 29 bytes = 521us - 2518us @ 115200bps |
| 132 | + |
| 133 | +class cCMD_SENDCAMERACOMMAND(cSTorM32RcCmd): |
| 134 | + |
| 135 | + def __init__(self,ser=None,payload=None,noresponse=True): |
| 136 | + super().__init__(ser,payload,noresponse) |
| 137 | + self.cmd = b'\x1B' |
| 138 | + |
| 139 | + def setPayload(self,payload): |
| 140 | + plen = len(payload) |
| 141 | + if plen < 1 or plen > 24: |
| 142 | + self.len = b'\x00' |
| 143 | + self.payload = b'' |
| 144 | + return False |
| 145 | + self.len = len(payload).to_bytes(1,'big') |
| 146 | + self.payload = payload |
| 147 | + return True |
| 148 | + |
| 149 | + |
| 150 | + |
| 151 | + |
| 152 | + |
| 153 | + |
| 154 | + |
0 commit comments