8
8
9
9
import gc
10
10
import os
11
+ import capnp
11
12
import numpy as np
12
- from typing import NoReturn
13
+ from typing import List , NoReturn , Optional
13
14
14
15
from cereal import log
15
16
import cereal .messaging as messaging
@@ -46,20 +47,20 @@ class Calibration:
46
47
INVALID = 2
47
48
48
49
49
- def is_calibration_valid (rpy ) :
50
+ def is_calibration_valid (rpy : np . ndarray ) -> bool :
50
51
return (PITCH_LIMITS [0 ] < rpy [1 ] < PITCH_LIMITS [1 ]) and (YAW_LIMITS [0 ] < rpy [2 ] < YAW_LIMITS [1 ])
51
52
52
53
53
- def sanity_clip (rpy ) :
54
+ def sanity_clip (rpy : np . ndarray ) -> np . ndarray :
54
55
if np .isnan (rpy ).any ():
55
56
rpy = RPY_INIT
56
57
return np .array ([rpy [0 ],
57
58
np .clip (rpy [1 ], PITCH_LIMITS [0 ] - .005 , PITCH_LIMITS [1 ] + .005 ),
58
59
np .clip (rpy [2 ], YAW_LIMITS [0 ] - .005 , YAW_LIMITS [1 ] + .005 )])
59
60
60
61
61
- class Calibrator () :
62
- def __init__ (self , param_put = False ):
62
+ class Calibrator :
63
+ def __init__ (self , param_put : bool = False ):
63
64
self .param_put = param_put
64
65
65
66
# Read saved calibration
@@ -80,7 +81,7 @@ def __init__(self, param_put=False):
80
81
self .reset (rpy_init , valid_blocks )
81
82
self .update_status ()
82
83
83
- def reset (self , rpy_init = RPY_INIT , valid_blocks = 0 , smooth_from = None ):
84
+ def reset (self , rpy_init : np . ndarray = RPY_INIT , valid_blocks : int = 0 , smooth_from : Optional [ np . ndarray ] = None ) -> None :
84
85
if not np .isfinite (rpy_init ).all ():
85
86
self .rpy = RPY_INIT .copy ()
86
87
else :
@@ -95,7 +96,7 @@ def reset(self, rpy_init=RPY_INIT, valid_blocks=0, smooth_from=None):
95
96
96
97
self .idx = 0
97
98
self .block_idx = 0
98
- self .v_ego = 0
99
+ self .v_ego = 0.0
99
100
100
101
if smooth_from is None :
101
102
self .old_rpy = RPY_INIT
@@ -104,13 +105,13 @@ def reset(self, rpy_init=RPY_INIT, valid_blocks=0, smooth_from=None):
104
105
self .old_rpy = smooth_from
105
106
self .old_rpy_weight = 1.0
106
107
107
- def get_valid_idxs (self ):
108
+ def get_valid_idxs (self ) -> List [ int ] :
108
109
# exclude current block_idx from validity window
109
110
before_current = list (range (self .block_idx ))
110
111
after_current = list (range (min (self .valid_blocks , self .block_idx + 1 ), self .valid_blocks ))
111
112
return before_current + after_current
112
113
113
- def update_status (self ):
114
+ def update_status (self ) -> None :
114
115
valid_idxs = self .get_valid_idxs ()
115
116
if valid_idxs :
116
117
rpys = self .rpys [valid_idxs ]
@@ -137,16 +138,16 @@ def update_status(self):
137
138
if self .param_put and write_this_cycle :
138
139
put_nonblocking ("CalibrationParams" , self .get_msg ().to_bytes ())
139
140
140
- def handle_v_ego (self , v_ego ) :
141
+ def handle_v_ego (self , v_ego : float ) -> None :
141
142
self .v_ego = v_ego
142
143
143
- def get_smooth_rpy (self ):
144
+ def get_smooth_rpy (self ) -> np . ndarray :
144
145
if self .old_rpy_weight > 0 :
145
146
return self .old_rpy_weight * self .old_rpy + (1.0 - self .old_rpy_weight ) * self .rpy
146
147
else :
147
148
return self .rpy
148
149
149
- def handle_cam_odom (self , trans , rot , trans_std ) :
150
+ def handle_cam_odom (self , trans : List [ float ] , rot : List [ float ] , trans_std : List [ float ]) -> Optional [ np . ndarray ] :
150
151
self .old_rpy_weight = min (0.0 , self .old_rpy_weight - 1 / SMOOTH_CYCLES )
151
152
152
153
straight_and_fast = ((self .v_ego > MIN_SPEED_FILTER ) and (trans [0 ] > MIN_SPEED_FILTER ) and (abs (rot [2 ]) < MAX_YAW_RATE_FILTER ))
@@ -176,7 +177,7 @@ def handle_cam_odom(self, trans, rot, trans_std):
176
177
177
178
return new_rpy
178
179
179
- def get_msg (self ):
180
+ def get_msg (self ) -> capnp . lib . capnp . _DynamicStructBuilder :
180
181
smooth_rpy = self .get_smooth_rpy ()
181
182
extrinsic_matrix = get_view_frame_from_road_frame (0 , smooth_rpy [1 ], smooth_rpy [2 ], model_height )
182
183
@@ -189,11 +190,11 @@ def get_msg(self):
189
190
msg .liveCalibration .rpyCalibSpread = self .calib_spread .tolist ()
190
191
return msg
191
192
192
- def send_data (self , pm ) -> None :
193
+ def send_data (self , pm : messaging . PubMaster ) -> None :
193
194
pm .send ('liveCalibration' , self .get_msg ())
194
195
195
196
196
- def calibrationd_thread (sm = None , pm = None ) -> NoReturn :
197
+ def calibrationd_thread (sm : Optional [ messaging . SubMaster ] = None , pm : Optional [ messaging . PubMaster ] = None ) -> NoReturn :
197
198
gc .disable ()
198
199
set_realtime_priority (1 )
199
200
@@ -223,7 +224,7 @@ def calibrationd_thread(sm=None, pm=None) -> NoReturn:
223
224
calibrator .send_data (pm )
224
225
225
226
226
- def main (sm = None , pm = None ) -> NoReturn :
227
+ def main (sm : Optional [ messaging . SubMaster ] = None , pm : Optional [ messaging . PubMaster ] = None ) -> NoReturn :
227
228
calibrationd_thread (sm , pm )
228
229
229
230
0 commit comments