-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDroneGroundStation.class.py
More file actions
213 lines (176 loc) · 7.01 KB
/
DroneGroundStation.class.py
File metadata and controls
213 lines (176 loc) · 7.01 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import pygame
import time
import serial
class DroneGroundStation:
"""
Main class for the Drone Ground Station responsible for controller input and data transmission.
The class initializes the pygame joystick and serial communication to COM4.
It processes stick inputs to calculate elevon positions and throttle values.
"""
def __init__(self):
"""
Initializes the ground station, sets default parameters and opens serial port.
Initializes pygame joystick (0) and serial connection at 115200 baud rate.
Sets default offsets for elevons: rightPar(-21) and leftPar(8).
"""
self.i=0
self.pad=None
self.left=76
self.right=76
self.rightPar=-21
self.leftPar=8
self.ly=0
self.rx=0
self.ry=0
self.throttle=0
self.speed=False
self.verbose=False
self.dpad=[]
self.dpad_old=[]
self.btn_str="-1"
self.btn_str_old=None
pygame.init()
pygame.joystick.init()
if pygame.joystick.get_count()==0:
print("Nie wykryto kontrolera")
exit()
self.pad=pygame.joystick.Joystick(0)
self.pad.init()
print("Kontroler:",self.pad.get_name())
time.sleep(4)
self.ser=serial.Serial('COM4',115200,timeout=0.1)
def __scaleControlSurfaces(self,val):
"""
Scales raw joystick axis value to control surface range.
val: raw axis value from -1.0 to 1.0.
return: scaled integer value for servo position.
"""
return round(val*18)
def __scaleThrottle(self,val):
"""
Scales raw joystick axis value to throttle range (0-180).
val: raw axis value from -1.0 to 1.0.
return: absolute throttle value.
"""
if val>0:
return 0
return abs(int(val*180))
def printPad(self):
"""Prints raw stick and button data to the console."""
print(f"lewy:{self.ly}, prawy: {self.rx} {self.ry}, przycisk: {self.btn_str}")
def padRead(self):
"""
Reads current events from pygame and updates internal state of axes and buttons.
Fetches 'ly', 'rx', 'ry' values and identifies which buttons are currently pressed.
Updates 'btn_str' with comma-separated button indices.
"""
for event in pygame.event.get():
if event.type==pygame.QUIT:
pygame.quit()
exit()
self.ly=self.__scaleThrottle(self.pad.get_axis(1))
self.rx=self.__scaleControlSurfaces(self.pad.get_axis(2))
self.ry=self.__scaleControlSurfaces(self.pad.get_axis(3))
self.btn_str_old=self.btn_str
pressed_buttons=[str(k) for k in range(self.pad.get_numbuttons()) if self.pad.get_button(k)]
if pressed_buttons:
self.btn_str=",".join(pressed_buttons)
else:
self.btn_str="-1"
self.dpad=[self.pad.get_hat(k) for k in range(self.pad.get_numhats())]
def elevonCalc(self):
"""
Calculates left and right elevon positions using stick mixing logic.
Uses 'ry' (pitch) and 'rx' (roll) to determine final 'left' and 'right' servo values
including 'leftPar' and 'rightPar' offsets.
"""
self.left=76+self.leftPar
self.right=76+self.rightPar
self.left+=self.ry+1*self.rx
self.right+=-1*self.ry+1*self.rx
def ElevonConfig(self):
"""
Enters a loop to configure elevon offsets using the D-pad.
Allows real-time adjustment of 'leftPar' and 'rightPar'.
The mode is exited when button '3' is pressed.
"""
print("Elevon Config Mode Activated!")
while True:
self.padRead()
time.sleep(0.2)
if ("3" in self.btn_str) and (self.btn_str_old!=self.btn_str):
print("Exiting Elevon Config Mode")
break
if self.dpad and self.dpad[0]==(-1,0):
self.leftPar-=1
self.left-=1
print(f"Left Elevon offset: {self.leftPar}")
if self.dpad and self.dpad[0]==(1,0):
self.leftPar+=1
self.left+=1
print(f"Left Elevon offset: {self.leftPar}")
if self.dpad and self.dpad[0]==(0,1):
self.rightPar+=1
self.right+=1
print(f"Right Elevon offset: {self.rightPar}")
if self.dpad and self.dpad[0]==(0,-1):
self.rightPar-=1
self.right-=1
print(f"Right Elevon offset: {self.rightPar}")
self.sendData()
def throttleChange(self):
"""
Manages throttle logic including cruise control (Tempomat).
If 'speed' (Tempomat) is ON, throttle is adjusted by 5 units using the D-pad.
Otherwise, throttle follows the 'ly' axis input.
"""
if ("4" in self.btn_str) and (self.btn_str_old!=self.btn_str):
self.speed=not self.speed
print(f"Tempomat: {'ON' if self.speed else 'OFF'}")
if not self.speed:
self.throttle=self.ly
return
if self.dpad!=self.dpad_old:
if self.dpad and self.dpad[0]==(0,1):
self.throttle+=5
elif self.dpad and self.dpad[0]==(0,-1):
self.throttle-=5
self.dpad_old=self.dpad[:]
self.throttle=max(0,min(180,self.throttle))
def sendData(self):
"""
Encodes 'throttle', 'left' and 'right' values into a frame and sends it via serial.
The frame format is 'T:val;L:val;R:val\n' encoded in ASCII.
"""
frame=f"T:{self.throttle};L:{int(self.left)};R:{int(self.right)}\n"
self.ser.write(frame.encode('ascii'))
self.ser.flush()
def printData(self):
"""Prints calculated control data (elevons and throttle) to the console."""
print(f"lEl:{self.left}, pEL: {self.right}, Throttle: {self.throttle}")
def Start(self):
"""
Main loop for the ground station.
Handles timing (50Hz), updates pad reading, calculates flight surfaces,
manages UI verbosity and sends data frames to the drone.
"""
while True:
self.padRead()
if "3" in self.btn_str:
self.ElevonConfig()
if ("2" in self.btn_str) and (self.btn_str_old!=self.btn_str):
self.verbose=not self.verbose
self.throttleChange()
if self.verbose:
self.printPad()
self.elevonCalc()
if self.i%50==0:
self.printData()
self.sendData()
time.sleep(0.02)
self.i+=1
if self.i==10000:
self.i=0
if __name__ == "__main__":
gs=DroneGroundStation()
gs.Start()