forked from simondlevy/PyQuadSim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpyquadsim_server.py
executable file
·166 lines (109 loc) · 4.63 KB
/
pyquadsim_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
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
159
160
161
162
163
164
165
166
#!/usr/bin/env python
'''
pyquadsim_server.py - Automatically-launched Python server script for PyQuadSim
Translates simulation values from V-REP to sensor values for quadrotor model
Copyright (C) 2014 Bipeen Acharya, Fred Gisa, and Simon D. Levy
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
'''
# Import your controller here =====================================================
#from quadstick import ExtremePro3D as Controller
from quadstick import PS3 as Controller
#from quadstick.rc.spektrum import DX8 as Controller
#from quadstick.rc.frsky import Taranis as Controller
#from quadstick.keyboard import Keyboard as Controller
# Mission-specific data ===========================================================
from pyquadsim_server_extra import getAdditionalData
# Simulation parameters ===========================================================
# Timeout for receiving data from client
TIMEOUT_SEC = 1.0
# Other imports ===================================================================
from sys import argv, exit
from math import pi
import struct
import time
import random
import os
from socket_server import serve_socket
from fmu import FMU
# Helper functions ================================================================
def sendFloats(client, data):
client.send(struct.pack('%sf' % len(data), *data))
def unpackFloats(msg, nfloats):
return struct.unpack('f'*nfloats, msg)
def receiveFloats(client, nfloats):
# We use 32-bit floats
msgsize = 4 * nfloats
# Implement timeout
start_sec = time.time()
remaining = msgsize
msg = ''
while remaining > 0:
msg += client.recv(remaining)
remaining -= len(msg)
if (time.time()-start_sec) > TIMEOUT_SEC:
return None
return unpackFloats(msg, nfloats)
def receiveString(client):
return client.recv(int(receiveFloats(client, 1)[0]))
def scalarTo3D(s, a):
return [s*a[2], s*a[6], s*a[10]]
# LogFile class ======================================================================================================
class LogFile(object):
def __init__(self, directory):
self.fd = open(directory + '/' + time.strftime('%d_%b_%Y_%H_%M_%S') + '.csv', 'w')
def writeln(self, string):
self.fd.write(string + '\n')
self.fd.flush()
def close(self):
self.fd.close()
# Initialization =====================================================================================================
# Require controller
controller = Controller(('Stabilize', 'Hold Altitude', 'Unused'))
# Serve a socket on the port indicated in the first command-line argument
client = serve_socket(int(argv[1]))
# Receive working directory path from client
pyquadsim_directory = receiveString(client)
# Create logs folder if needed
logdir = pyquadsim_directory + '/logs'
if not os.path.exists(logdir):
os.mkdir(logdir)
# Open logfile named by current date, time
logfile = LogFile(logdir)
# Create an FMU object, passing it the logfile object in case it needs to write to the logfile.
fmu = FMU(logfile)
# Loop ==========================================================================================================
prevtime = time.time()
# Forever loop will be halted by VREP client or by exception
while True:
try:
currtime = time.time()
print(currtime - prevtime)
prevtime = currtime
# Get core data from client
coreData = receiveFloats(client, 4)
# Quit on timeout
if not coreData: exit(0)
# Get extra data from client
extraData = getAdditionalData(client, receiveFloats)
# Unpack IMU data
timestep = coreData[0] # seconds
pitch = coreData[1] # positive = nose up
roll = coreData[2] # positive = right down
yaw = coreData[3] # positive = nose right
# Poll controller
demands = controller.poll()
# Get motor thrusts from quadrotor model
thrusts = fmu.getMotors((pitch, roll, yaw), demands, timestep, extraData)
# Send thrusts to client
sendFloats(client, thrusts)
except Exception:
# Inform and exit on exception
controller.error()
exit(0)