-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLaptop_Fernsteuerung.py
103 lines (79 loc) · 2.61 KB
/
Laptop_Fernsteuerung.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
import socket
from PCA9685 import PCA9685
from AlphaBot2 import AlphaBot2
pwm=PCA9685(0x40)
pwm.setPWMFreq(50)
#Set the Horizontal servo parameters
HPulse = 1050 #Sets the initial Pulse 50=> 80 grad rechts, 1050 => 0 1850=> 80 grad links
HStep = 0 #Sets the initial step length
pwm.setServoPulse(0,HPulse)
#Set the vertical servo parameters
VPulse = 1050 #Sets the initial Pulse 50 => 85 grad oben, 1050 gerade 1500 45 grad unten
VStep = 0 #Sets the initial step length
pwm.setServoPulse(1,VPulse)
HOST = '0.0.0.0' # Server IP or Hostname
PORT = 12352 # Pick an open Port (1000+ recommended), must match the client sport
def degree_to_Pulse(angle,min_angle=-80,max_angle=80,min_pulse=50,max_pulse=1850,min_allowed_pulse=50,max_allowed_pulse=1850):
print('angle: ',angle)
angle=-angle ## positive angles => right or up
pulse = (angle-min_angle)/(max_angle-min_angle)*(max_pulse-min_pulse)+min_pulse
if pulse>max_allowed_pulse:
pulse = max_allowed_pulse
if pulse<min_allowed_pulse:
pulse = int(min_allowed_pulse)
return(pulse)
if __name__=="__main__":
# initialize Alpha Bot
AlphaBot = AlphaBot2()
# initialize serial connection to laptop
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print ('Socket created')
#managing error exception
try:
s.bind((HOST, PORT))
except socket.error:
print('Bind failed ')
s.listen(5)
print('Socket awaiting messages')
conn, addr = s.accept()
print('Connected')
# awaiting for message
while True:
data = conn.recv(1024).decode()
print('I sent a message back in response to: ' + data)
reply = ''
# process your message
if data == 'Hello':
reply = 'Hi, back!'
elif data == 'This is important':
reply = 'OK, I have done the important thing you have asked me!'
elif data.startswith('servo'):
try:
h=int(data.split(' ')[1])
h_pulse = int(degree_to_Pulse(h))
v=int(data.split(' ')[2])
v_pulse = int(degree_to_Pulse(v,max_allowed_pulse=1500))
pwm.setServoPulse(0,h_pulse)
pwm.setServoPulse(1,v_pulse)
reply = 'servo set to h_pulse='+str(h)+' v_pulse='+str(v)
except:
reply = 'servo message corrupt'
elif data.startswith('motor'):
try:
left=int(data.split(' ')[1])
right=int(data.split(' ')[2])
print(left,right)
AlphaBot.setMotor(left, right)
reply = 'motor set to left '+str(left)+' right='+str(right)
except:
reply = 'servo message corrupt'
#and so on and on until...
elif data == 'quit':
conn.send('Terminating')
break
else:
reply = 'Unknown command'
# Sending reply
reply = bytes(reply,'utf-8')
conn.send(reply)
conn.close() # Close connections