-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTurretControl.py
More file actions
75 lines (54 loc) · 1.59 KB
/
TurretControl.py
File metadata and controls
75 lines (54 loc) · 1.59 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
import pygame
import pyfirmata # Firmata is a standard Arduino Serial communication protocol
# arduino firmata setup
arduino = pyfirmata.Arduino('COM5')
# set up yaw and pitch servo pins
Yaw = arduino.get_pin('d:3:s')
Pitch = arduino.get_pin('d:4:s')
# angles
yaw = 0
pitch = 0
# angles for updating
angles = [yaw, pitch]
oldAngles = angles
# divisors
XDIV = 7
YDIV = 4
# screen dimensions
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
CANVAS_SIZE = (SCREEN_WIDTH, SCREEN_HEIGHT)
WHITE = (255, 255, 255)
# create screen
canvas = pygame.display.set_mode(CANVAS_SIZE)
# screen caption
pygame.display.set_caption("Laser Turret")
# create lines on screen
pygame.draw.line(canvas, WHITE, (0, SCREEN_HEIGHT / 2), (SCREEN_WIDTH, SCREEN_HEIGHT / 2))
pygame.draw.line(canvas, WHITE, (SCREEN_WIDTH / 2, 0), (SCREEN_WIDTH / 2, SCREEN_HEIGHT))
# Pygame Loop
clock = pygame.time.Clock()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
# get mouse positions
(mouseX, mouseY) = pygame.mouse.get_pos()
# possible tuning
# mouseY += 10z
# servo angles (coords -> angles)
yaw = int(mouseX * (182 / SCREEN_WIDTH))
pitch = int(mouseY * (182 / SCREEN_HEIGHT))
# update angles
angles = [yaw, pitch]
# serial send if updated angles
if angles != oldAngles:
oldAngles = angles
# serial send (angles -> PWM thingy)
Yaw.write(yaw * (255 / 182))
Pitch.write(pitch * (255 / 182))
print(yaw, pitch)
# pygame update
clock.tick(60)
pygame.display.flip()