Skip to content

Commit d1b6440

Browse files
committed
add rudimentary STorM32 Python library
1 parent eeb2033 commit d1b6440

File tree

2 files changed

+217
-0
lines changed

2 files changed

+217
-0
lines changed

py-library/STorM32_lib.py

+154
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
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+

py-library/storm32-test.py

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
2+
import time, os, sys, math
3+
import serial
4+
from STorM32_lib import *
5+
6+
7+
uart = "COM23"
8+
uart = "COM21"
9+
#uart = "COM9"
10+
baud = 115200
11+
12+
display = True
13+
#display = False
14+
15+
16+
17+
if len(sys.argv) > 1:
18+
print(sys.argv[1])
19+
if sys.argv[1] == '57' or sys.argv[1] == '57600':
20+
baud = 57600
21+
if sys.argv[1] == '115' or sys.argv[1] == '115200':
22+
baud = 115200
23+
if sys.argv[1] == '921' or sys.argv[1] == '921600':
24+
baud = 921600
25+
26+
27+
ser = serial.Serial(uart, baud)
28+
t1Hz_last = time.perf_counter()
29+
30+
pitch = 0.0
31+
pitch_dir = 1.0
32+
yaw = 0.0
33+
yaw_dir = 1.0
34+
35+
36+
while True:
37+
38+
tnow = time.perf_counter()
39+
if tnow - t1Hz_last > 1.0:
40+
t1Hz_last += 1.0
41+
if display:
42+
print('-- 1Hz --')
43+
44+
# cmd = cCMD_GETVERSION(ser)
45+
# cmd = cCMD_GETVERSIONSTR(ser)
46+
# cmd = cCMD_SENDCAMERACOMMAND(ser)
47+
# cmd.setPayload(b'\x01\x02\x03')
48+
# cmd = cCMD_SETANGLES(ser)
49+
# cmd.setPayload(pitch,0.0,yaw)
50+
cmd = cCMD_SETANGLES(ser,pitch,0.0,yaw)
51+
cmd.send()
52+
print(">- ", cmd.getCmd())
53+
if pitch >= 45.0: pitch_dir = -1.0
54+
if pitch <= -45.0: pitch_dir = 1.0
55+
pitch += pitch_dir * 15.0
56+
if yaw >= 45.0: yaw_dir = -1.0
57+
if yaw <= -45.0: yaw_dir = 1.0
58+
yaw += yaw_dir * 7.5
59+
60+
available = ser.in_waiting
61+
if available > 0:
62+
c = ser.read(available)
63+
print("<- ", c)

0 commit comments

Comments
 (0)