This repository has been archived by the owner on Apr 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathrcManager.py
218 lines (169 loc) · 7.47 KB
/
rcManager.py
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
214
215
216
217
#
# Handles RC (radio control) connection state and IO.
# Created by Will Silva on 3/5/2016.
# Updated by Jason Short 4/6/2016.
# Copyright (c) 2016 3D Robotics.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import os
import socket
import sys
import shotLogger
from sololink import rc_pkt
sys.path.append(os.path.realpath(''))
from shotManagerConstants import *
from sololink import rc_ipc
from sololink import rc_pkt
import shots
import rcManager
logger = shotLogger.logger
DEFAULT_RC_MIN = 1000
DEFAULT_RC_MAX = 2000
DEFAULT_RC_MID = ( DEFAULT_RC_MAX - DEFAULT_RC_MIN ) / 2.0 + DEFAULT_RC_MIN
# These channels have different ranges
CHANNEL6_MIN = 1000
CHANNEL6_MAX = 1520
CHANNEL6_MID = ( CHANNEL6_MAX - CHANNEL6_MIN ) / 2.0 + CHANNEL6_MIN
CHANNEL8_MIN = 0
CHANNEL8_MAX = 1000
CHANNEL8_MID = ( CHANNEL8_MAX - CHANNEL8_MIN ) / 2.0 + CHANNEL8_MIN
THROTTLE_FAILSAFE = 900
DEAD_ZONE = 0.009
TICKS_UNTIL_RC_STALE = 20 # ticks
RC_TCP_BUFSIZE = 1024
class rcManager():
def __init__(self, shotmgr):
self.shotmgr = shotmgr
self.connected = False
self.bindServer()
# Manage flow of RC data
self.numTicksSinceRCUpdate = 0
self.timestamp = 0
self.sequence = 0
self.channels = None
# True if we have RC link
self.failsafe = False
if os.path.exists( "/run/rc_uplink_cmd" ):
self.server.sendto("attach", "/run/rc_uplink_cmd")
# only log this once
self.loggedRC_ipc = False
# We are sending the RC data to PixRC and disabling CH Yaw
# Force off on startup
self.remappingSticks = True
self.enableRemapping(False)
def bindServer(self):
# set up a socket to receive rc inputs from pixrc
if os.path.exists( "/tmp/shotManager_RCInputs_socket" ):
os.remove( "/tmp/shotManager_RCInputs_socket" )
self.server = socket.socket( socket.AF_UNIX, socket.SOCK_DGRAM )
self.server.bind("/tmp/shotManager_RCInputs_socket")
self.server.setblocking(0)
# This is called whenever we have data on the rc socket, which should be
# at 50 hz, barring any drops
# We remap/normalize the RC and then store it away for use in Tick()
def parse(self):
datagram = self.server.recv(RC_TCP_BUFSIZE)
if datagram == None or len(datagram) != rc_pkt.LENGTH:
self.channels = None
return
self.timestamp, self.sequence, self.channels = rc_pkt.unpack(datagram)
if self.channels[THROTTLE] > THROTTLE_FAILSAFE:
self.numTicksSinceRCUpdate = 0
else:
# we are in failsafe so don't cache data - we'll send defaults
self.channels = None
def rcCheck(self):
self.numTicksSinceRCUpdate += 1
if self.numTicksSinceRCUpdate > TICKS_UNTIL_RC_STALE:
if self.failsafe == False:
logger.log( "[RC] Enter failsafe")
self.triggerFailsafe(True)
else:
if self.failsafe == True:
logger.log( "[RC] Exit failsafe")
self.triggerFailsafe(False)
def isRcConnected(self):
return self.numTicksSinceRCUpdate < TICKS_UNTIL_RC_STALE
"""
This remaps all of our RC input into (-1.0, 1.0) ranges.
The RC channels come in as PWM values of (1000, 2000)
Filtered camera paddle is in channel 6 (index 5), and has range (1000, 1520) for some odd reason.
Raw camera paddle is channel 8 (index 7) and is of range (0, 1000)
This function is responsible for sending RC data back to PixRC for remapping purposes.
However, it will only do this if the sendToPixRC param is set to True and self.remappingSticks is set
"""
def remap(self):
if self.failsafe or self.channels == None:
# send default values to the Pixhawk
self.channels = [DEFAULT_RC_MID, DEFAULT_RC_MID, THROTTLE_FAILSAFE, DEFAULT_RC_MID, DEFAULT_RC_MIN, CHANNEL6_MAX, DEFAULT_RC_MIN, CHANNEL8_MID ]
normChannels = [0]*8
# channels 1-4
for i in range(4):
normChannels[i] = self.normalizeRC( self.channels[i], DEFAULT_RC_MIN, DEFAULT_RC_MAX )
#logger.log("FP %d, RP %d" % (self.channels[FILTERED_PADDLE], self.channels[RAW_PADDLE]))
# channel 6 (index 5) is the filtered gimbal paddle value
# its values go from CHANNEL6_MIN - CHANNEL6_MAX
# this value is used directly to point the gimbal
# 1520 = level, 1000 = straight down
normChannels[FILTERED_PADDLE] = self.normalizeRC( self.channels[FILTERED_PADDLE], CHANNEL6_MIN, CHANNEL6_MAX )
# channel 8 (index 7) is the raw gimbal paddle and is a special case
# its values go from CHANNEL8_MIN - CHANNEL8_MAX
# this value is used in smart shots where the pitch paddle is used for altitude up/down (such as in zipline free look)
# >500 = tilt up, 500 = no tilt, < 500 tilt down
normChannels[RAW_PADDLE] = self.normalizeRC( self.channels[RAW_PADDLE], CHANNEL8_MIN, CHANNEL8_MAX)
if self.remappingSticks:
# never allow Yaw to rotate in guided shots to prevent shot confusion
self.channels[YAW] = 1500
if not rc_ipc.put((self.timestamp, self.sequence, self.channels)):
if not self.loggedRC_ipc:
logger.log( "ERROR returned from rc_ipc.put" )
self.loggedRC_ipc = True
return normChannels
# convert from RC input values to (-1.0, 1.0) floating point value
# min/max is customizable to handle inputs of different ranges
def normalizeRC(self, value, min, max):
# failsafe is out of these bounds
if value < min or value > max:
return 0.0
halfrange = (max - min) / 2.0
midpt = halfrange + min
# this is our range
effectiveRange = halfrange
# scale the input to (-1.0, 1.0),
result = float( value - midpt ) / effectiveRange
if abs(result) < DEAD_ZONE:
return 0.0
else:
return result
def triggerFailsafe(self, doFailsafe):
self.failsafe = doFailsafe
if self.failsafe:
self.shotmgr.enterFailsafe()
# enable/Disable remapping of sticks
def enableRemapping(self, doEnable):
# only act on change
if self.remappingSticks != doEnable:
self.remappingSticks = doEnable
if os.path.exists( "/run/rc_uplink_cmd" ):
if doEnable:
logger.log("[RC] Enabling stick remapping")
rc_ipc.attach()
self.server.sendto("detach uplink", "/run/rc_uplink_cmd")
else:
logger.log("[RC] Disabling stick remapping")
self.server.sendto("attach uplink", "/run/rc_uplink_cmd")
rc_ipc.detach()
def detach(self):
logger.log( "[RC] detach from rc_ipc.put" )
if os.path.exists( "/run/rc_uplink_cmd" ):
self.server.sendto("detach", "/run/rc_uplink_cmd")
self.enableRemapping(False)