Skip to content

Commit f8b798c

Browse files
committed
moved the joystick parsing code into UserInput.py
1 parent ef8fac1 commit f8b798c

4 files changed

Lines changed: 73 additions & 79 deletions

File tree

run_robot.py

Lines changed: 17 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
import pigpio
2+
import numpy as np
3+
import UDPComms
4+
import time
25
from src.Controller import step_controller, Controller
36
from src.HardwareInterface import send_servo_commands, initialize_pwm
47
from src.PupperConfig import (
@@ -9,16 +12,16 @@
912
ServoParams,
1013
PWMParams,
1114
)
12-
import time
13-
import numpy as np
14-
import UDPComms
15+
from src.UserInput import UserInputs, get_input, update_controller
1516

1617

1718
def main():
1819
"""Main program
1920
"""
2021
pi_board = pigpio.pi()
2122
pwm_params = PWMParams()
23+
initialize_pwm(pi_board, pwm_params)
24+
2225
servo_params = ServoParams()
2326

2427
controller = Controller()
@@ -34,68 +37,31 @@ def main():
3437
controller.stance_params = StanceParams()
3538
controller.stance_params.delta_y = 0.08
3639
controller.gait_params = GaitParams()
37-
controller.gait_params.dt = 0.01
3840

39-
initialize_pwm(pi_board, pwm_params)
41+
user_input = UserInputs()
4042

41-
values = UDPComms.Subscriber(8830, timeout=0.3)
4243
last_loop = time.time()
4344
now = last_loop
4445
start = time.time()
4546

46-
gait_mode = 0 # 0 for non-walking, 1 for walking
47-
prev_gait_toggle = 0
48-
49-
non_walking_gait = np.array(
50-
[[1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1]]
51-
)
52-
walking_gait = np.array([[1, 1, 1, 0], [1, 0, 1, 1], [1, 0, 1, 1], [1, 1, 1, 0]])
53-
5447
for i in range(60000):
5548
last_loop = time.time()
56-
step_controller(controller)
57-
send_servo_commands(pi_board, pwm_params, servo_params, controller.joint_angles)
58-
59-
try:
60-
msg = values.get()
61-
except UDPComms.timeout:
62-
print("timeout")
63-
msg = {
64-
"x": 0,
65-
"y": 0,
66-
"twist": 0,
67-
"pitch": 0,
68-
"gait_toggle": 0,
69-
"stance_movement": 0,
70-
}
71-
x_vel = msg["y"] / 7.0
72-
y_vel = -msg["x"] / 7.0
73-
yaw_rate = -msg["twist"] * 0.8
74-
gait_toggle = msg["gait_toggle"]
75-
stance_movement = msg["stance_movement"]
76-
77-
# Check for gait toggle
78-
if prev_gait_toggle == 0 and gait_toggle == 1:
79-
gait_mode = not gait_mode
80-
prev_gait_toggle = gait_toggle
49+
50+
# Parse the udp joystick commands and then update the robot controller's parameters
51+
get_input(user_input)
52+
update_controller(controller, user_input)
8153

82-
pitch = msg["pitch"] * 30.0 * np.pi / 180.0
83-
84-
controller.movement_reference.v_xy_ref = np.array([x_vel, y_vel])
85-
controller.movement_reference.wz_ref = yaw_rate
86-
controller.movement_reference.pitch = pitch
87-
88-
if gait_mode == 0:
89-
controller.gait_params.contact_phases = non_walking_gait
90-
else:
91-
controller.gait_params.contact_phases = walking_gait
54+
# Step the controller forward by dt
55+
step_controller(controller)
9256

93-
# Note this is negative since it is the feet relative to the body
94-
controller.movement_reference.z_ref -= 0.001 * stance_movement
57+
# Update the pwm widths going to the servos
58+
send_servo_commands(pi_board, pwm_params, servo_params, controller.joint_angles)
9559

60+
# Wait until it's time to execute again
9661
while now - last_loop < controller.gait_params.dt:
9762
now = time.time()
9863
# print("Time since last loop: ", now - last_loop)
64+
9965
end = time.time()
10066
print("seconds per loop: ", (end - start) / 1000.0)
10167

src/Controller.py

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ def __init__(self):
2222

2323
self.ticks = 0
2424

25+
# Set default for foot locations and joint angles
2526
self.foot_locations = (
2627
self.stance_params.default_stance
2728
+ np.array([0, 0, self.movement_reference.z_ref])[:, np.newaxis]
@@ -111,30 +112,4 @@ def step_controller(controller):
111112
controller.joint_angles = four_legs_inverse_kinematics(
112113
rotated_foot_locations, controller.robot_config
113114
)
114-
controller.ticks += 1
115-
116-
117-
def run():
118-
"""Testing function that runs the robot for one second.
119-
120-
Returns
121-
-------
122-
(Numpy array (3, 4, timesteps), Numpy array (3, 4, timesteps))
123-
(history of foot locations, history of joint angles)
124-
"""
125-
c = Controller()
126-
c.movement_reference.v_xy_ref = np.array([0.2, 0.0])
127-
c.movement_reference.wz_ref = 0.5
128-
129-
tf = 1.0
130-
time_steps = int(tf / c.gait_params.dt)
131-
132-
foot_loc_history = np.zeros((3, 4, time_steps))
133-
joint_angle_history = np.zeros((3, 4, time_steps))
134-
135-
for i in range(time_steps):
136-
step_controller(c)
137-
foot_loc_history[:, :, i] = c.foot_locations
138-
joint_angle_history[:, :, i] = c.joint_angles
139-
140-
return foot_loc_history, joint_angle_history
115+
controller.ticks += 1

src/PupperConfig.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import numpy as np
2-
import numpy as np
32
from scipy.linalg import solve
43

54

src/UserInput.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import UDPComms
2+
import numpy as np
3+
4+
5+
class UserInputs:
6+
def __init__(self, udp_port=8830):
7+
self.x_vel = 0.0
8+
self.y_vel = 0.0
9+
self.yaw_rate = 0.0
10+
self.pitch = 0.0
11+
self.stance_movement = 0
12+
self.gait_toggle = 0
13+
self.gait_mode = 0
14+
self.previous_gait_toggle = 0
15+
self.udp_handle = UDPComms.Subscriber(udp_port, timeout=0.3)
16+
17+
18+
def get_input(user_input_obj):
19+
try:
20+
msg = user_input_obj.udp_handle.get()
21+
user_input_obj.x_vel = msg["y"] * 0.14
22+
user_input_obj.y_vel = msg["x"] * -0.14
23+
user_input_obj.yaw_rate = msg["twist"] * -0.8
24+
user_input_obj.pitch = msg["pitch"] * 30 * np.pi / 180.0
25+
user_input_obj.gait_toggle = msg["gait_toggle"]
26+
user_input_obj.stance_movement = msg["stance_movement"]
27+
28+
# Update gait mode
29+
if user_input_obj.previous_gait_toggle == 0 and user_input_obj.gait_toggle == 1:
30+
user_input_obj.gait_mode = not user_input_obj.gait_mode
31+
user_input_obj.previous_gait_toggle = user_input_obj.gait_toggle
32+
33+
except UDPComms.timeout:
34+
print("UDP Timed out")
35+
36+
37+
def update_controller(controller, user_input_obj):
38+
controller.movement_reference.v_xy_ref = np.array(
39+
[user_input_obj.x_vel, user_input_obj.y_vel]
40+
)
41+
controller.movement_reference.wz_ref = user_input_obj.yaw_rate
42+
controller.movement_reference.pitch = user_input_obj.pitch
43+
44+
if user_input_obj.gait_mode == 0:
45+
controller.gait_params.contact_phases = np.array(
46+
[[1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1]]
47+
)
48+
else:
49+
controller.gait_params.contact_phases = np.array(
50+
[[1, 1, 1, 0], [1, 0, 1, 1], [1, 0, 1, 1], [1, 1, 1, 0]]
51+
)
52+
53+
# Note this is negative since it is the feet relative to the body
54+
controller.movement_reference.z_ref -= 0.001 * user_input_obj.stance_movement

0 commit comments

Comments
 (0)