-
Notifications
You must be signed in to change notification settings - Fork 11
/
TinyKeyboardSSH.py
116 lines (87 loc) · 3.5 KB
/
TinyKeyboardSSH.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
#!/usr/bin/env python
# coding: Latin-1
# Load library functions we want
from sys import exit
import atexit
import curses
from explorerhat import motor
# --------------------------------------------------------------------------------------------------------
# Info
# Purpose: Control a Tiny4WD using keyboard keys within a ssh-session
# Editor : Stefan Theurer [email protected]
# Thanks for the well written recipe on
# https://gpiozero.readthedocs.io/en/stable/recipes.html#keyboard-controlled-robot
# ------------------------------------------------------------------------------------------
# Python Libraries
# You may need to install these Python dependencies:
# sudo pip3 install explorerhat
# The max speed setting for the motors.
MAX_SPEED=100
# Depending on which way round the positive and negative wires are connected to the motors,
# one or both of these values may need to be negative, e.g. -MAX_POWER
LEFT_MOTOR_MAX_SPEED_FWD = MAX_SPEED # The top speed that makes the left motors turn in the forward direction
RIGHT_MOTOR_MAX_SPEED_FWD = -MAX_SPEED # The top speed that makes the right motors turn in the forward direction
# Depending on which side of your robot motors are mounted these might need swapping
left_motor = motor.two # The explorer pHAT 'motor.one' is connected to the robots left hand motors
right_motor = motor.one # The explorer pHAT 'motor.two' is connected to the robots right hand motors
# --------------------------------------------------------------------------------------------------------
# Utilities
# Run this whenever the script exits
@atexit.register
def cleanup():
# disable all motors
motor.stop()
print("Bye")
# --------------------------------------------------------------------------------------------------------
# Motor Control
# You shouldn't need to change these, but if the robot is not going in the direction you expect
# please see the settings section above and adjust the LEFT_MOTOR_MAX_POWER_FWD, RIGHT_MOTOR_MAX_POWER_FWD
# left_motor & right_motor settings there.
def forward():
left_motor.speed(LEFT_MOTOR_MAX_SPEED_FWD)
right_motor.speed(RIGHT_MOTOR_MAX_SPEED_FWD)
def backward():
left_motor.speed(-LEFT_MOTOR_MAX_SPEED_FWD)
right_motor.speed(-RIGHT_MOTOR_MAX_SPEED_FWD)
def left():
left_motor.speed(-LEFT_MOTOR_MAX_SPEED_FWD)
right_motor.speed(RIGHT_MOTOR_MAX_SPEED_FWD)
def right():
left_motor.speed(LEFT_MOTOR_MAX_SPEED_FWD)
right_motor.speed(-RIGHT_MOTOR_MAX_SPEED_FWD)
def stop():
left_motor.speed(0)
right_motor.speed(0)
def end():
exit(0)
# --------------------------------------------------------------------------------------------------------
# Keyboard Actions
actions = {
# Arrow keys:
curses.KEY_UP: forward,
curses.KEY_DOWN: backward,
curses.KEY_LEFT: left,
curses.KEY_RIGHT: right,
}
# --------------------------------------------------------------------------------------------------------
# Main
def main(window):
next_key = None
while True:
if next_key is None:
key = window.getch()
else:
key = next_key
next_key = None
if key != -1:
# KEY DOWN
curses.halfdelay(1)
action = actions.get(key)
if action is not None:
action()
next_key = key
while next_key == key:
next_key = window.getch()
# KEY UP
stop()
curses.wrapper(main)