-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontrol.py
More file actions
84 lines (62 loc) · 2.36 KB
/
control.py
File metadata and controls
84 lines (62 loc) · 2.36 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
from math import degrees, radians
from pybrickspc.messaging import BluetoothMailboxClient, Mailbox
import pygame
import struct
import sys
import time
import config
# This is the address of the server EV3 we are connecting to.
SERVER = "f0:45:da:13:1c:8a"
_mailbox_client = BluetoothMailboxClient()
print("establishing connection...")
_mailbox_client.connect(SERVER)
print("connected!")
_direction = Mailbox(config.direction_channel, _mailbox_client)
def input_gain(x: float):
return 0.4 * x + 0.6 * x**3
last_send = None
def handle_stick_input(j: pygame.joystick.JoystickType):
global last_send
if last_send is not None:
if time.time() - last_send < 0.1:
return
last_send = time.time()
left_right = int(100 * j.get_axis(0))
forward_backward = int(100 * j.get_axis(1))
_direction.send(struct.pack(config.direction_format, left_right, forward_backward))
print("pygame init")
pygame.init()
print("pygame init done")
def main():
# Used to manage how fast the screen updates.
clock = pygame.time.Clock()
joysticks = {}
while True:
# Event processing step.
# Possible joystick events: JOYAXISMOTION, JOYBALLMOTION, JOYBUTTONDOWN,
# JOYBUTTONUP, JOYHATMOTION, JOYDEVICEADDED, JOYDEVICEREMOVED
for event in pygame.event.get():
# Handle hotplugging
if event.type == pygame.JOYDEVICEADDED:
# This event will be generated when the program starts for every
# joystick, filling up the list without needing to create them manually.
joy = pygame.joystick.Joystick(event.device_index)
joysticks[joy.get_instance_id()] = joy
print(f"Joystick {joy.get_instance_id()} connencted")
if event.type == pygame.JOYDEVICEREMOVED:
del joysticks[event.instance_id]
print(f"Joystick {event.instance_id} disconnected")
if pygame.joystick.get_count() == 0:
# Don't do anything unless there is a joystick
clock.tick(1)
print("no joy")
continue
for j in joysticks.values():
handle_stick_input(j)
# Limit to 30 frames per second.
clock.tick(60)
if __name__ == "__main__":
main()
# If you forget this line, the program will 'hang'
# on exit if running from IDLE.
pygame.quit()