aboutsummaryrefslogtreecommitdiff
path: root/robot/test/dummy.py
blob: 5265fa0efbce314611f55049f6c815cb32d0c62c (plain)
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
import pygame
import serial
pygame.init()
joysticks = []
clock = pygame.time.Clock()
keepPlaying = True

left_right_stick = 0
forward_stick = 0
backward_stick = 0

def sign(num):
  if num == 0:
    return 0
  elif num > 0:
    return 1
  else:
    return -1

serport = serial.Serial("/dev/ttyACM0", 9600, timeout=1)
def send_speed(left, right):
  total = ""
  total += f"{(sign(left) + 1):02x}"
  total += f"{(abs(left)):02x}"
  total += f"{(sign(right) + 1):02x}"
  total += f"{(abs(right)):02x}"

  serport.write(bytes(total, 'utf-8'))
  serport.flush()
  print(total)

for i in range(0, pygame.joystick.get_count()):
  joysticks.append(pygame.joystick.Joystick(i))
  joysticks[-1].init()
  print ("Detected joystick "),joysticks[-1].get_name(),"'"

def make_values():
  left = 255 - max(0, left_right_stick) * 255
  right = 255 + min(0, left_right_stick) * 255
  speed_mod = backward_stick + forward_stick
  return (int(left * speed_mod) , int(right * speed_mod))

while keepPlaying:
  clock.tick(60)
  values = make_values()
  send_speed(values[0], values[1])
  for event in pygame.event.get():
    if event.type != 1536: continue
    if event.axis == 0: left_right_stick = event.value
    if event.axis == 2: backward_stick = event.value * -0.5 - 1
    if event.axis == 5: forward_stick = event.value * 0.5 + 1