-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathjoystick_client.py
More file actions
158 lines (117 loc) · 4.25 KB
/
joystick_client.py
File metadata and controls
158 lines (117 loc) · 4.25 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
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
"""
AUTHOR:
Gregory Ferguson & Benjamin Knight
WHAT IS THIS:
This python program is to drive the UTA Mars Rover while
wirelessly connected (sockets) to the Rover's driving arduino with the standard
firmata sketch uploaded to it. This scheme uses a wired joystick, controlled through pygame's joystick layout.
OTHER JOYSTICKS MAY NOT BE MAPPED THE SAME.
JOYSTICK SCHEME:
y-axis = power
x-axis = turning
7 button = kill program
Making senese of the "power" values:
0 - 0.49 | motors in "reverse", 0 is full power
0.51 - 1 | motors in "forwards", 1 is full power
0.5 | motors is not moving
"""
import socket
import threading
import ast
import time
import pygame
from pygame.locals import *
# Raspberry Pi's IP on network it projects
HOST = '192.168.4.1'
# function to senf strings to server
def send(threadName, socket):
# joystick initialization
pygame.init()
joysticks = []
for i in range(pygame.joystick.get_count()):
joysticks.append(pygame.joystick.Joystick(i))
joysticks[-1].init()
controlling_joystick = joysticks[0]
reverse = False
while True:
pygame.event.pump()
if(controlling_joystick.get_button(6)):
print("Exiting...")
exit()
"""
OLD CONTROLLER CODE
# divider ensure only half power given unless "A" button pressed down
divider = 0.125
reverse = 0
right_power = controlling_joystick.get_axis(2)
left_power = controlling_joystick.get_axis(5)
# "A" button pressed, give full power
if(controlling_joystick.get_button(0)):
divider = 0.25
right_power = divider * (right_power + 1)
left_power = divider * (left_power + 1)
# Bumpers are pressed, go reverse. Must be doing both to avoid breaking bot
if(controlling_joystick.get_button(4) and controlling_joystick.get_button(5)):
left_power = 0.5 - left_power
right_power = 0.5 - right_power
reverse = 1
else:
left_power = 0.5 + left_power
right_power = 0.5 + right_power
"""
# both start at same power level
# remap power from (-1,1) to (0,1), 0 full reverse-1 full forward
power = controlling_joystick.get_axis(1)
left_power = power
right_power = power
turn_modifier = controlling_joystick.get_axis(0)
# this is to give the motors different power levels (skid steering)
alpha = 0.5 * (turn_modifier + 1)
# TURNING RIGHT
if(turn_modifier > 0):
if( alpha < 0.5 ):
right_power = right_power = right_power * alpha
left_power = left_power * (1-alpha)
else:
right_power = right_power = right_power * (1-alpha)
left_power = left_power * alpha
# TURNING LEFT
elif(turn_modifier < 0):
if( alpha > 0.5 ):
right_power = right_power = right_power * alpha
left_power = left_power * (1-alpha)
else:
right_power = right_power = right_power * (1-alpha)
left_power = left_power * alpha
# NOT TURNING DO NOTHING
else:
pass
right_power = remap(right_power, -1, 1, 0, 1)
left_power = remap(left_power, -1, 1, 0, 1)
# construct packet string array
data = [str(round(left_power, 2)), str(round(right_power, 2)), str(reverse)]
print(data)
sent = socket.send(str(data).encode('utf8'))
if(sent == 0):
raise RuntimeError("socket connection broken")
time.sleep(.1)
del data[:]
def receive(threadName, socket):
data = socket.recv(1024)
strings = data.decode('utf8')
#Converting string to list
res = strings.strip('][').split(', ')
print(res)
#At this point, will check to see what the data from server is and update the GUI
def main():
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, 5000))
print("Connected")
# t2 = threading.Thread(target = receive, args = ("Thread-2", s))
#
# t2.start()
t1 = threading.Thread(target = send, args = ("Thread-1", s))
t1.start()
t1.join()
if __name__=="__main__":
main()