1+ # mypy: ignore-errors
12import math
23
34from controller import Keyboard
4- from sbot import AnalogPins , Robot
5+ from sbot import AnalogPin , Colour , arduino , comp , leds , motors , utils , vision
56
67# Any keys still pressed in the following period will be handled again
78# leading to rprinting sensors multiple times
@@ -62,17 +63,17 @@ def angle_str(angle: float) -> str:
6263 return f"{ angle :.4f} rad"
6364
6465
65- def print_sensors (robot : Robot ) -> None :
66+ def print_sensors () -> None :
6667 ultrasonic_sensor_names = {
6768 (2 , 3 ): "Front" ,
6869 (4 , 5 ): "Left" ,
6970 (6 , 7 ): "Right" ,
7071 (8 , 9 ): "Back" ,
7172 }
7273 reflectance_sensor_names = {
73- AnalogPins .A0 : "Left" ,
74- AnalogPins .A1 : "Center" ,
75- AnalogPins .A2 : "Right" ,
74+ AnalogPin .A0 : "Left" ,
75+ AnalogPin .A1 : "Center" ,
76+ AnalogPin .A2 : "Right" ,
7677 }
7778 touch_sensor_names = {
7879 10 : "Front Left" ,
@@ -83,29 +84,30 @@ def print_sensors(robot: Robot) -> None:
8384
8485 print ("Distance sensor readings:" )
8586 for (trigger_pin , echo_pin ), name in ultrasonic_sensor_names .items ():
86- dist = robot . arduino .ultrasound_measure (trigger_pin , echo_pin )
87+ dist = arduino .measure_ultrasound_distance (trigger_pin , echo_pin )
8788 print (f"({ trigger_pin } , { echo_pin } ) { name : <12} : { dist :.0f} mm" )
8889
8990 print ("Touch sensor readings:" )
9091 for pin , name in touch_sensor_names .items ():
91- touching = robot . arduino .pins [ pin ]. digital_value
92+ touching = arduino .digital_read ( pin )
9293 print (f"{ pin } { name : <6} : { touching } " )
9394
9495 print ("Reflectance sensor readings:" )
9596 for Apin , name in reflectance_sensor_names .items ():
96- reflectance = robot . arduino .pins [ Apin ]. analog_value
97+ reflectance = arduino .analog_read ( Apin )
9798 print (f"{ Apin } { name : <12} : { reflectance :.2f} V" )
9899
99100
100- def print_camera_detection (robot : Robot ) -> None :
101- markers = robot . camera . see ()
101+ def print_camera_detection () -> None :
102+ markers = vision . detect_markers ()
102103 if markers :
103104 print (f"Found { len (markers )} makers:" )
104105 for marker in markers :
105106 print (f" #{ marker .id } " )
106107 print (
107- f" Position: { marker .distance :.0f} mm, azi: { angle_str (marker .azimuth )} , "
108- f"elev: { angle_str (marker .elevation )} " ,
108+ f" Position: { marker .position .distance :.0f} mm, "
109+ f"{ angle_str (marker .position .horizontal_angle )} right, "
110+ f"{ angle_str (marker .position .vertical_angle )} up" ,
109111 )
110112 yaw , pitch , roll = marker .orientation
111113 print (
@@ -119,11 +121,15 @@ def print_camera_detection(robot: Robot) -> None:
119121 print ()
120122
121123
122- robot = Robot ()
123-
124124keyboard = KeyboardInterface ()
125125
126- key_sense = CONTROLS ["sense" ][robot .zone ]
126+ # Automatically set the zone controls based on the robot's zone
127+ # Alternatively, you can set this manually
128+ # ZONE_CONTROLS = 0
129+ ZONE_CONTROLS = comp .zone
130+
131+ assert ZONE_CONTROLS < len (CONTROLS ["forward" ]), \
132+ "No controls defined for this zone, alter the ZONE_CONTROLS variable to use in this zone."
127133
128134print (
129135 "Note: you need to click on 3D viewport for keyboard events to be picked "
@@ -138,36 +144,42 @@ def print_camera_detection(robot: Robot) -> None:
138144 keys = keyboard .process_keys ()
139145
140146 # Actions that are run continuously while the key is held
141- if CONTROLS ["forward" ][robot . zone ] in keys ["held" ]:
147+ if CONTROLS ["forward" ][ZONE_CONTROLS ] in keys ["held" ]:
142148 left_power += 0.5
143149 right_power += 0.5
144150
145- if CONTROLS ["reverse" ][robot . zone ] in keys ["held" ]:
151+ if CONTROLS ["reverse" ][ZONE_CONTROLS ] in keys ["held" ]:
146152 left_power += - 0.5
147153 right_power += - 0.5
148154
149- if CONTROLS ["left" ][robot . zone ] in keys ["held" ]:
155+ if CONTROLS ["left" ][ZONE_CONTROLS ] in keys ["held" ]:
150156 left_power -= 0.25
151157 right_power += 0.25
152158
153- if CONTROLS ["right" ][robot . zone ] in keys ["held" ]:
159+ if CONTROLS ["right" ][ZONE_CONTROLS ] in keys ["held" ]:
154160 left_power += 0.25
155161 right_power -= 0.25
156162
157- if CONTROLS ["boost" ][robot . zone ] in keys ["held" ]:
163+ if CONTROLS ["boost" ][ZONE_CONTROLS ] in keys ["held" ]:
158164 boost = True
159165
160166 # Actions that are run once when the key is pressed
161- if CONTROLS ["sense" ][robot .zone ] in keys ["pressed" ]:
162- print_sensors (robot )
163-
164- if CONTROLS ["see" ][robot .zone ] in keys ["pressed" ]:
165- print_camera_detection (robot )
166-
167- if CONTROLS ["led" ][robot .zone ] in keys ["pressed" ]:
168- pass
169-
170- if CONTROLS ["angle_unit" ][robot .zone ] in keys ["pressed" ]:
167+ if CONTROLS ["sense" ][ZONE_CONTROLS ] in keys ["pressed" ]:
168+ print_sensors ()
169+
170+ if CONTROLS ["see" ][ZONE_CONTROLS ] in keys ["pressed" ]:
171+ print_camera_detection ()
172+
173+ if CONTROLS ["led" ][ZONE_CONTROLS ] in keys ["pressed" ]:
174+ leds .set_colour (0 , Colour .MAGENTA )
175+ leds .set_colour (1 , Colour .MAGENTA )
176+ leds .set_colour (2 , Colour .MAGENTA )
177+ elif CONTROLS ["led" ][ZONE_CONTROLS ] in keys ["released" ]:
178+ leds .set_colour (0 , Colour .OFF )
179+ leds .set_colour (1 , Colour .OFF )
180+ leds .set_colour (2 , Colour .OFF )
181+
182+ if CONTROLS ["angle_unit" ][ZONE_CONTROLS ] in keys ["pressed" ]:
171183 USE_DEGREES = not USE_DEGREES
172184 print (f"Angle unit set to { 'degrees' if USE_DEGREES else 'radians' } " )
173185
@@ -176,7 +188,7 @@ def print_camera_detection(robot: Robot) -> None:
176188 left_power = max (min (left_power * 2 , 1 ), - 1 )
177189 right_power = max (min (right_power * 2 , 1 ), - 1 )
178190
179- robot . motor_board . motors [ 0 ]. power = left_power
180- robot . motor_board . motors [ 1 ]. power = right_power
191+ motors . set_power ( 0 , left_power )
192+ motors . set_power ( 1 , right_power )
181193
182- robot .sleep (KEYBOARD_SAMPLING_PERIOD / 1000 )
194+ utils .sleep (KEYBOARD_SAMPLING_PERIOD / 1000 )
0 commit comments