-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
88 lines (76 loc) · 2.7 KB
/
main.py
File metadata and controls
88 lines (76 loc) · 2.7 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
'''
main.py is the main program runnning on the raspberry pi. It should
be started as a system process upon bootup.
Here's how it works
1. Main infinite loop:
a. defines important config variables and state variables
b. nested infinite while loop (1/2):
- This is for the disabled config
- Reads xbox values, sees what state the robot should be in (disabled/walking/etc)
- If no longer disabled, switch to command loop
c. nested infinite while loop (2/2):
- This is the command loop
- commands are sent to robot at a certain sampling rate (dt)
- Checks to see time since last command, skips interation until at least dt time has passed
- Does one iteration of stuff
- Rechecks mode from xbox controller
'''
# imports
from time import sleep, time
from src.xbox_input import input_loop, ControllerState, controllerState
import threading
import src.top_controller as TopController
import src.State as State
import config as Configuration
from src.command import Command
def x_test():
print("X pressed")
def y_test():
print("Y pressed")
def b_test():
print("B pressed")
def a_test():
print("A pressed")
# Access to controller and command:
# Controller: xDown(), getCommand(), setButtonCallback(), getLS()
# Command:
def main():
thread = threading.Thread(target=input_loop)
thread.start()
controllerState.setButtonCallback('x', x_test)
controllerState.setButtonCallback('y', y_test)
controllerState.setButtonCallback('b', b_test)
controllerState.setButtonCallback('a', a_test)
# Create top level controller
top_controller = TopController()
#Initializing state
state = State()
# Initializing configuration
config = Configuration()
# MAIN LOOP #
while True:
# Loop to wait for a command
# print(controllerState.getLS()) # Print Left stick values
print("Waiting for Button A to Activate robot")
last_loop = time.time()
while True:
# Read command value from controller
command = controllerState.getCommand()
if command.activate_event == 1:
break
sleep(0.1)
print("ROBOT ACTIVATED")
# Command Loop
while True:
# Run gait controller, based on various inputs
# Read in controller command
now = time.time()
# Check to see if a full cycle time has passed
if now - last_loop < config.dt:
# if not, skip this iteration of the loop
continue
last_loop = time.time()
if command.activate_event == 1:
break
print("ROBOT DEACTIVATED")
main()