-
Notifications
You must be signed in to change notification settings - Fork 2
/
Calibration.py
201 lines (170 loc) · 7.33 KB
/
Calibration.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
import core
import time
import cwiid
import os.path
from ConfigParser import SafeConfigParser
# from lib_oled96 import ssd1306
from core import ServoEnum
try:
from collections import OrderedDict
except ImportError:
from ordereddict import OrderedDict
class Calibration:
def __init__(self, core_module, wm, oled):
"""Class Constructor"""
self.filename = "motors.ini"
self.killed = False
self.core = core_module
self.wiimote = wm
self.oled = oled
self.ticks = 0
# Note: string desciptions on the right are for
# reference only. Not currently used in this code.
self.menu_list = OrderedDict((
(ServoEnum.LEFT_MOTOR_ESC, "Left Motor ESC"),
(ServoEnum.RIGHT_MOTOR_ESC, "Right Motor ESC"),
(ServoEnum.LEFT_AUX_ESC, "Left Aux ESC"),
(ServoEnum.RIGHT_AUX_ESC, "Right Aux ESC"),
(ServoEnum.LEFT_FLINGER_ESC, "Left Flinger ESC"),
(ServoEnum.RIGHT_FLINGER_ESC, "Right Flinger ESC"),
(ServoEnum.LEFT_FLINGER_SERVO, "Left Flinger Servo"),
(ServoEnum.RIGHT_FLINGER_SERVO, "Right flinger Servo"),
(ServoEnum.WINCH_SERVO, "Winch Servo")
))
# Default current mode to NONE
self.mode = ServoEnum.LEFT_MOTOR_ESC
def get_next_mode(self, mode):
""" Find the previous menu item """
mode_index = self.menu_list.keys().index(mode)
next_index = mode_index + 1
if next_index >= len(self.menu_list):
next_index = 0 # Wrapped round to end
return self.menu_list.keys()[next_index]
def get_previous_mode(self, mode):
""" Find the previous menu item """
mode_index = self.menu_list.keys().index(mode)
previous_index = mode_index - 1
if previous_index < 0:
previous_index = len(self.menu_list) - 1 # Wrapped round to end
return self.menu_list.keys()[previous_index]
def show_servo_config(self, servo_id):
""" Show servo config """
if self.oled is not None:
servo = self.core.servos[servo_id]
title = "{}:".format(servo[2])
message = "{} / {} / {}".format(
str(servo[0].servo_min),
str(servo[0].servo_mid),
str(servo[0].servo_max)
)
# Clear Screen
self.oled.cls()
self.oled.canvas.text((10, 10), title, fill=1)
self.oled.canvas.text((10, 30), message, fill=1)
# Now show the mesasge on the screen
self.oled.display()
def stop(self):
"""Simple method to stop the RC loop"""
self.killed = True
def run(self):
""" Main Challenge method. Has to exist and is the
start point for the threaded challenge. """
adjust_value = 5
# Sleep a small time to allow screen to refresh in launcher code
time.sleep(0.25)
# Show servo config for current item.
self.show_servo_config(self.mode)
# Loop indefinitely, or until this thread is flagged as stopped.
while self.wiimote and not self.killed:
value_adjusted = False
# While in RC mode, get joystick states and pass speeds to motors.
classic_buttons_state = self.wiimote.get_classic_buttons()
if classic_buttons_state is not None:
if (classic_buttons_state & cwiid.CLASSIC_BTN_LEFT):
self.mode = self.get_previous_mode(self.mode)
value_adjusted = True
if (classic_buttons_state & cwiid.CLASSIC_BTN_RIGHT):
self.mode = self.get_next_mode(self.mode)
value_adjusted = True
if (classic_buttons_state & cwiid.CLASSIC_BTN_PLUS):
self.core.servos[self.mode][0].adjust_range(adjust_value)
value_adjusted = True
if (classic_buttons_state & cwiid.CLASSIC_BTN_MINUS):
self.core.servos[self.mode][0].adjust_range(-adjust_value)
value_adjusted = True
# Show current config
if value_adjusted:
self.show_servo_config(self.mode)
# Send motors "stick neutral" so that we can test centre value
self.core.throttle(0.0, 0.0)
# Sleep between loops to allow other stuff to
# happen and not over burden Pi and Arduino.
time.sleep(0.05)
def read_config(self):
""" Read the motor defaults from the config file. """
print("Reading Config")
# Only bother reading if file exists
if os.path.isfile(self.filename):
# Get config file
config = SafeConfigParser()
config.read(self.filename)
# Read the motor min/mid/max servo ranges
if self.core is not None:
# Read in min/mid/max of menu items that we can change.
for item in self.menu_list.items():
item_enum_str = str(item[0]).split('.')
item_min_str = "{}_MIN".format(item_enum_str[0])
item_mid_str = "{}_MID".format(item_enum_str[0])
item_max_str = "{}_MAX".format(item_enum_str[0])
try:
self.core.servos[item[0]][0].servo_min = \
int(config.get('motors', item_min_str))
self.core.servos[item[0]][0].servo_mid = \
int(config.get('motors', item_mid_str))
self.core.servos[item[0]][0].servo_max = \
int(config.get('motors', item_max_str))
except:
print("Failed to read item from ini.")
print("Finished Reading Config")
def write_config(self):
""" Read the motor defaults from the config file. """
self.filename = "motors.ini"
# Get config file
config = SafeConfigParser()
# Only bother reading if file exists
if os.path.isfile(self.filename):
config.read(self.filename)
# *** Server Settings ***
try:
config.add_section('motors')
except:
print("Failed to add, could already exist")
# Write out min/mid/max of menu items that we can change.
for item in self.menu_list.items():
item_enum_str = str(item[0]).split('.')
item_min_str = "{}_MIN".format(item_enum_str[0])
item_mid_str = "{}_MID".format(item_enum_str[0])
item_max_str = "{}_MAX".format(item_enum_str[0])
config.set(
'motors',
item_min_str,
str(self.core.servos[item[0]][0].servo_min))
config.set(
'motors',
item_mid_str,
str(self.core.servos[item[0]][0].servo_mid))
config.set(
'motors',
item_max_str,
str(self.core.servos[item[0]][0].servo_max))
if __name__ == "__main__":
core = core.Core()
calibration = Calibration(core)
try:
calibration.run_auto()
except (KeyboardInterrupt) as e:
# except (Exception, KeyboardInterrupt) as e:
# Stop any active threads before leaving
calibration.stop()
core.set_neutral()
print("Quitting")