-
Notifications
You must be signed in to change notification settings - Fork 9
/
yourrobot.py
134 lines (98 loc) · 4.28 KB
/
yourrobot.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
# This class inherits from the BrickPi interface, it should include any code for sub-routines
# You can also over-ride any functions that you do not like. The BrickPiInterface is code created by your teacher to make using the robot easier. It is composed of snippets of code from the DexterIndustries github
from interfaces.brickpiinterface import BrickPiInterface
from interfaces.databaseinterface import DatabaseHelper
import logging
import time
ENABLED = 1
DISABLED = 5 #if the sensor returns NOREADING more than 5 times in a row, its permanently
class Robot(BrickPiInterface):
def __init__(self, timelimit=30, database=None):
super().__init__(timelimit)
self.database = database #database handle, use ViewQueryHelper, ModifyQueryHelper
self.CurrentRoutine = 'ready' #could be useful to keep track of the current routine
return
#Override this function if you wish to change the Ports
def set_ports(self):
bp = self.BP
self.rightmotor = bp.PORT_A
self.leftmotor = bp.PORT_B
self.largemotors = bp.PORT_A + bp.PORT_B
self.mediummotor = bp.PORT_C
self.thermal = bp.PORT_1 #Thermal infrared Sensor
self.colour = bp.PORT_2 #Colour Sensor
self.ultra = bp.PORT_4 #ultraSonic Sensor
self.claw_closed = False #Current state of the claw
self.thermal_thread = None #DO NOT REMOVE THIS - USED LATER
#self.gyro = bp.PORT_3 #lego Gyro Sensor - replaced with IMU sensor
self.configure_sensors() #CONFIGURES SENSORS!!! from BrickPi Interface
bp.set_motor_limits(self.mediummotor, 100, 600) #set medium motor power / speed limit
return
#gets the current routine
def get_current_routine(self):
return self.CurrentRoutine
#set the database inside the robot
def set_database(self, database):
self.database = database
return
#create a function that will find a path to the victim and save path events to the database
def find_path_victim(self):
if self.CurrentRoutine != "ready":
return
self.CurrentRoutine = "find_path_victim"
#insert your code here
self.CurrentRoutine = "ready"
return
#rescue the victim using the claw.
def rescue_victim(self):
if self.CurrentRoutine != "ready":
return
self.CurrentRoutine = "rescue_victim"
#insert your code here
self.CurrentRoutine = "ready"
return
#use the saved path data to determine a way back to the start
def return_victim_to_start(self):
if self.CurrentRoutine != "ready":
return
self.CurrentRoutine = "return_victim_to_start"
#insert your code here
self.CurrentRoutine = "ready"
return
#------------POSSIBLE FUNCTIONS TO USE OR OVERRIDE--------------------#
#def calibrate_imu(self, timelimit=20)
#def reconfig_IMU(self)
#def get_compass_IMU(self)
#def get_orientation_IMU(self)
#def get_linear_acceleration_IMU(self)
#def get_gyro_sensor_IMU(self)
#def get_temperature_IMU(self)
#def get_gyro_sensor_EV3(self)
#def get_ultra_sensor(self)
#def get_colour_sensor(self)
#def get_thermal_sensor(self, usethread=True)
#def get_battery(self)
#def move_power(self, power)
#def move_power_time(self, power, t)
#def move_power_untildistanceto(self, power, distanceto)
#def rotate_power_time(self, power, t)
#def rotate_power_degrees_IMU(self, power, degrees, marginoferror=3)
#def rotate_power_heading_IMU(self, power, targetheading, marginoferror=3)
#def open_claw(self, degrees=-1100)
#def close_claw(self, degrees=1100)
#def log(self, message)
#def stop_all(self)
#def get_current_command(self)
#def get_all_sensors(self)
#def safe_exit(self)
#--------------------------------------------------------------------
#Only execute if this is the main file, this section is good for testing code
if __name__ == '__main__':
robot = Robot(timelimit=20)
logger = logging.getLogger()
robot.set_log(logger)
robot.calibrate_imu(timelimit=10) #calibration might requirement movement
input("Press any key to test")
robot.log(robot.get_all_sensors())
robot.move_power_untildistanceto(30,10)
robot.safe_exit()