-
Notifications
You must be signed in to change notification settings - Fork 0
/
receiver.py
90 lines (73 loc) · 2.39 KB
/
receiver.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
"""
This app will receiver a UDP protocol package and decode this one to
control connected motors in a RaspberryPi.
By Allex Lima <allexlima@unn.edu.br> | http://allexlima.com
Jun 2017, MIT License
"""
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import socket
import struct
import RPi.GPIO as gpio
class Receiver(object):
def __init__(self, ip, port):
self.address = (ip, port)
self.udp = None
def __active(self):
self.udp = socket.socket(type=socket.SOCK_DGRAM)
self.udp.bind(self.address)
def listener(self, origin=None):
self.__active()
(package, origin_address) = self.udp.recvfrom(1024)
return None if (origin is not None) and (origin != origin_address[0]) else struct.unpack('>2h', package)
def deactivate(self):
self.udp.close()
class MotorsPi(object):
def __init__(self, pins):
gpio.setmode(gpio.BOARD)
if isinstance(pins, list) is False:
raise Exception("You must provide a list of GPIO pins!")
self.pins = pins
for pin in self.pins:
gpio.setup(pin, gpio.OUT)
def __set_pins(self, pins, state):
for i in pins:
gpio.output(self.pins[i], state)
def stopped(self):
self.__set_pins(range(4), False)
def forward(self):
self.__set_pins([1, 2], True)
self.__set_pins([0, 3], False)
def backward(self):
self.__set_pins([0, 3], True)
self.__set_pins([1, 2], False)
def right(self):
self.__set_pins([1, 3], True)
self.__set_pins([0, 2], False)
def left(self):
self.__set_pins([0, 2], True)
self.__set_pins([1, 3], False)
if __name__ == "__main__":
remote = Receiver('', 5000)
control = MotorsPi([37, 35, 38, 36])
security_origin = None
print("[On] Waiting for control inputs...")
while True:
data = remote.listener(security_origin)
if data == (1, 1):
print('forward')
control.forward()
elif data == (-1, -1):
print('backward')
control.backward()
elif data == (-1, 1):
print('left')
control.left()
elif data == (1, -1):
print('right')
control.right()
else:
print('stopped')
control.stopped()
remote.deactivate()
print("[Off] The controller service is down! Bye...")