-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.py
93 lines (74 loc) · 1.9 KB
/
server.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
import RPi.GPIO as GPIO
from socket import *
leftMotor = {'+': 13, '-': 11}
rightMotor = {'+': 5, '-': 3}
GPIO.setmode(GPIO.BOARD)
GPIO.setup(3, GPIO.OUT)
GPIO.setup(5, GPIO.OUT)
GPIO.setup(7, GPIO.OUT)
GPIO.setup(11, GPIO.OUT)
GPIO.setup(13, GPIO.OUT)
GPIO.setup(15, GPIO.OUT)
pwm1 = GPIO.PWM(7, 100)
pwm2 = GPIO.PWM(15, 100)
pwm1.start(0)
pwm2.start(0)
pwm1.changeDutyCycle(99)
pwm2.changeDutyCycle(99)
def leftMotorGo(dir):
print('left : ' + dir)
if dir is 'front':
GPIO.output(leftMotor['+'], True)
GPIO.output(leftMotor['-'], False)
else:
GPIO.output(leftMotor['+'], False)
GPIO.output(leftMotor['-'], False)
def rightMotorGo(dir):
print('right : ' + dir)
if dir is 'front':
GPIO.output(rightMotor['+'], True)
GPIO.output(rightMotor['-'], False)
else:
GPIO.output(rightMotor['+'], False)
GPIO.output(rightMotor['-'], False)
def up():
leftMotorGo('front')
rightMotorGo('front')
def down():
leftMotorGo('stop')
rightMotorGo('stop')
def left():
leftMotorGo('stop')
rightMotorGo('front')
def right():
leftMotorGo('front')
rightMotorGo('stop')
def setSpeed(val):
print("DutyCycle : " + str(val))
pwm1.changeDutyCycle(val)
pwm2.changeDutyCycle(val)
def main():
host = "192.168.0.15"
port = 13000
buf = 1024
addr = (host, port)
UDPSock = socket(AF_INET, SOCK_DGRAM)
UDPSock.bind(addr)
print('Waiting to receive messages...')
while True:
(data, addr) = UDPSock.recvfrom(buf)
data = data.decode('utf-8')
print('Received message: ' + data)
if data == 'start':
up()
elif data == 'stop':
down()
elif data == 'right':
right()
elif data == 'left':
left()
elif data[0:3] == "set":
setSpeed(int(data[3:]))
UDPSock.close()
os._exit(0)
main()