-
Notifications
You must be signed in to change notification settings - Fork 0
/
RPiServer_Test.py
79 lines (58 loc) · 2.18 KB
/
RPiServer_Test.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
if __name__ == "__main__" :
print("\033cStarting ...\n") # Clear Terminal
import socket
import sys
import os
try :
import dotenv
from dynamixel_sdk import *
except ModuleNotFoundError as Err:
missing_module = str(Err).replace('No module named ', '')
missing_module = missing_module.replace("'", '')
if missing_module == 'dynamixel_sdk':
sys.exit(f'No module named {missing_module} try : pip install dynamixel-sdk')
elif missing_module == 'dotenv':
sys.exit(f'No module named {missing_module} try : pip install python-dotenv')
else:
print(f'No module named {missing_module} try : pip install {missing_module}')
test = True # Set to True for debuging and testing
LINE_UP = '\033[1A'
LINE_CLEAR = '\x1b[2K'
dotenv.load_dotenv()
bufferSize = 1024
serverPort = int(os.getenv('serverPort_env'))
serverIP = os.getenv('serverIP_env')
RPi_Socket = socket.socket(socket.AF_INET,socket.SOCK_DGRAM) # Using UTPy
RPi_Socket.bind((serverIP,serverPort))
try :
Done = False
print('\033c',end='')
while not Done :
print('Server is Up and waiting ...')
messageReceived, clientAddress = RPi_Socket.recvfrom(bufferSize)
messageReceived = messageReceived.decode('utf-8')
print(LINE_UP,end=LINE_CLEAR)
#print(f'The message is : {messageReceived}')#\nFrom : \t\t\t{clientAddress[0]}\nOn port number {clientAddress[1]}')
if messageReceived.lower() == 'done' :
messageFromServer = 'Done Received'
messageFromServer_bytes = messageFromServer.encode('utf-8')
RPi_Socket.sendto(messageFromServer_bytes, clientAddress)
Done = True
elif messageReceived.lower() == 'grab' :
print('Grab')
# move_motor(1)
elif messageReceived.lower() == 'walk' :
print('Walk')
# Torque lock
elif messageReceived.lower() == 'down' :
print('Down')
# move_motor(-1)
else :
if test :
pass
else :
sys.exit('Unknown Message Received')
except KeyboardInterrupt :
pass
if __name__ == "__main__" :
print('\nProgramme Stopped\n')