-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrobot_control.py
More file actions
162 lines (128 loc) · 4.85 KB
/
robot_control.py
File metadata and controls
162 lines (128 loc) · 4.85 KB
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
import serial
import time
import numpy as np
class RobotControl:
def __init__(self, port='/dev/ttyACM0', baudrate=115200):
"""Initialize robot control interface"""
self.serial = serial.Serial(port, baudrate)
time.sleep(2) # Wait for Arduino to reset
# SBUS channel constants
self.LOW = 192
self.MID = 992
self.HIGH = 1792
self.dead_zone = 100
def send_command(self, cmd):
"""Send a command string to the robot"""
self.serial.write(f"{cmd}\n".encode())
time.sleep(0.02) # Small delay to ensure command is processed
def move(self, strafe, forward, turn, pitch=0):
"""Send movement command
Args:
strafe: Left/Right motion (-1 to 1)
forward: Forward/Backward motion (-1 to 1)
turn: Turning motion (-1 to 1)
pitch: Pitch up/down motion (-1 to 1)
"""
# Convert -1 to 1 range to SBUS values
strafe_val = int(self.MID + (strafe * (self.HIGH - self.MID)))
forward_val = int(self.MID + (forward * (self.HIGH - self.MID)))
turn_val = int(self.MID + (turn * (self.HIGH - self.MID)))
pitch_val = int(self.MID + (pitch * (self.HIGH - self.MID)))
# Constrain values
strafe_val = max(self.LOW, min(self.HIGH, strafe_val))
forward_val = max(self.LOW, min(self.HIGH, forward_val))
turn_val = max(self.LOW, min(self.HIGH, turn_val))
pitch_val = max(self.LOW, min(self.HIGH, pitch_val))
#print("forward: {} turn: {}".format(forward_val, turn_val))
self.send_command(f"move:{strafe_val},{forward_val},{turn_val},{pitch_val}")
def unlock(self):
"""Unlock the robot (Mode 1, Channel 7)"""
self.send_command("unlock")
def toggle_damping(self):
"""Toggle damping mode (Mode 1, Channel 6)"""
self.send_command("damping")
def recover(self):
"""Execute fall recovery (Mode 2, Channel 7)"""
self.send_command("recover")
def toggle_continuous_movement(self):
"""Toggle continuous movement (Mode 2, Channel 6)"""
self.send_command("continuous")
def toggle_stand_height(self):
"""Toggle between high and low stand heights (Mode 1, Channel 8)"""
self.send_command("stand")
def toggle_obstacle_avoidance(self):
"""Toggle obstacle avoidance mode (Mode 2, Channel 8)"""
self.send_command("obstacle")
def switch_gait(self):
"""Switch walking gait (Mode 3, Channel 8 high)"""
self.send_command("gait")
def toggle_light(self):
"""Toggle LED light (Mode 3, Channel 8 low)"""
self.send_command("light")
def dance(self):
"""Execute dance sequence (Mode 3, Channel 7)"""
self.send_command("dance")
def jump(self):
"""Execute jump sequence (Mode 3, Channel 6)"""
self.send_command("jump")
def stop(self):
"""Stop all motion"""
self.move(0, 0, 0, 0)
def close(self):
"""Close the serial connection"""
self.stop()
self.serial.close()
def main():
"""Example usage of the robot control interface"""
try:
# Initialize robot control
robot = RobotControl()
# Unlock the robot
print("Unlocking robot...")
robot.unlock()
time.sleep(1)
# Move forward
# print("Moving forward...")
# robot.move(0, 0.0, 0.5) # 50% forward speed
# time.sleep(3)
# # Stop
# print("Stopping...")
# robot.stop()
# time.sleep(1)
# # Execute some special commands
# print("Performing dance...")
# robot.dance()
# time.sleep(3)
# # Switch gait
# print("Switching gait...")
# robot.switch_gait()
# time.sleep(2)
# toggle light
# print("Toggling light...")
# robot.toggle_light()
# time.sleep(1)
# # toggle continuous movement
# print("Toggling continuous movement...")
# robot.toggle_continuous_movement()
# time.sleep(1)
# Move forward
print("Moving forward...")
start_time = time.time()
while time.time() - start_time < 2:
robot.move(0, -0.5, 0.5) # 50% forward speed
time.sleep(0.05)
# print("Jumping...")
# robot.jump()
# time.sleep(2)
# print("Toggling stand height...")
# robot.toggle_stand_height()
# time.sleep(2)
# print("Damping mode...")
# robot.toggle_damping()
# time.sleep(1)
except KeyboardInterrupt:
print("\nStopping robot...")
finally:
robot.close()
if __name__ == "__main__":
main()