Skip to content

Commit

Permalink
simplified scl api
Browse files Browse the repository at this point in the history
  • Loading branch information
TobiasSimon committed Jul 15, 2015
1 parent 0185bf0 commit c2104b3
Show file tree
Hide file tree
Showing 44 changed files with 107 additions and 600 deletions.
13 changes: 3 additions & 10 deletions ads1x15/service/ads1x15.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@
GNU General Public License for more details. """


from msgpack import dumps
from scl import generate_map
from scl import scl_get_socket
from opcd_interface import OPCD_Interface
from misc import daemonize
from time import sleep
Expand All @@ -47,20 +46,14 @@ def read(self):


def main(name):
map = generate_map(name)
socket = map['power']
socket = scl_get_socket('voltage', 'pub')
opcd = OPCD_Interface(map['opcd_ctrl'], 'pi_quad')
voltage_adc = ADS1x15_ADC(opcd.get('voltage_channel'))
#current_adc = ADS1x15_ADC(opcd.get('current_channel'))
voltage_lambda = eval(opcd.get('adc_to_voltage'))
#current_lambda = eval(opcd.get('adc_to_current'))
while True:
sleep(0.2)
voltage = voltage_lambda(voltage_adc.read())
#current = current_lambda(current_adc.read())
state = [voltage, # 0 [V]
0.4] # 1 [A]
socket.send(dumps(state))
socket.send([voltage])


daemonize('ads1x15', main)
Expand Down
4 changes: 2 additions & 2 deletions aircomm/service/aircomm.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def run(self):
addr = msg[0]
# if message is meant for us, forward to application(s):
if addr in [THIS_SYS_ID, BCAST, BCAST_NOFW]:
msg_scl = dumps(msg[1:]) # strip the type and pack again
msg_scl = msg[1:] # strip the type
self.scl_socket.send(msg_scl)

# if the message (a) is not meant for us or (b) is NOFW, re-broadcast:
Expand Down Expand Up @@ -105,7 +105,7 @@ def main(name):

# read from SCL in socket and send data via NRF
while True:
data = loads(in_socket.recv())
data = in_socket.recv()
if len(data) == 2:
msg = [data[0], THIS_SYS_ID, data[1]]
elif len(data) > 2:
Expand Down
35 changes: 17 additions & 18 deletions api/ctrl_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@


from scl import scl_get_socket
from msgpack import dumps


### PRIVATE API: ###
Expand All @@ -42,7 +41,7 @@ def set(self, state):
assert state == 0 or state == 1
if self.prev_state != state:
for s in self.sockets:
s.send(dumps(state))
s.send(state)
self.prev_state = state


Expand Down Expand Up @@ -105,37 +104,37 @@ def set(self, state):

def mot_en(val):
"""enables (True) or disables (False) the motors"""
_mot_en.send(dumps(int(val)))
_mot_en.send(int(val))


def set_ys(val):
"""sets the yaw speed in rad/s"""
_rp_y_oe.set(0)
_rs_sp_y.send(dumps(float(val)))
_rs_sp_y.send(float(val))


def set_yp(val):
"""sets yaw position in rad, North direction is 0, rotating right positive"""
_rp_y_oe.set(1)
_rp_sp_y.send(dumps(float(val)))
_rp_sp_y.send(float(val))


def set_thrust(val):
"""sets the overall thrust value in Newtons"""
_vs_oe.set(0)
_thrust.send(dumps(float(val)))
_thrust.send(float(val))


def set_thrust_max(val):
"""sets upper thrust limit, used as a safety feature in autonomous flight"""
_thrust_max.send(dumps(float(val)))
_thrust_max.send(float(val))


def set_vs(val):
"""sets vertical speed based on barometer speed estimate"""
_vs_oe.set(1)
_vp_oe.set(0)
_vs_sp.send(dumps(float(val)))
_vs_sp.send(float(val))


def set_vp(val, mode = 'ultra'):
Expand All @@ -146,31 +145,31 @@ def set_vp(val, mode = 'ultra'):
- evel: baro control accoding to SRTM elevation map (val: safety distance)"""
_vs_oe.set(1)
_vp_oe.set(1)
_vp_sp.send(dumps([str(mode), float(val)]))
_vp_sp.send([str(mode), float(val)])


def set_torques(vec):
"""sets 3D torques directly, without control; this is intended for debugging or calibration, not for flying!"""
_rs_oe.set(0)
vec = map(float, vec)
_torques.send(dumps(vec))
_torques.send(vec)


def set_rs(vec):
"""sets the rotation speed of pitch and roll"""
_rs_oe.set(1)
_rp_oe.set(0)
_rs_sp_p.send(dumps(float(vec[0])))
_rs_sp_r.send(dumps(float(vec[1])))
_rs_sp_p.send(float(vec[0]))
_rs_sp_r.send(float(vec[1]))


def set_rp(vec):
"""sets the rotation position of pitch and roll"""
_rs_oe.set(1)
_rp_oe.set(1)
_hs_oe.set(0)
_rp_sp_p.send(dumps(float(vec[0])))
_rp_sp_r.send(dumps(float(vec[1])))
_rp_sp_p.send(float(vec[0]))
_rp_sp_r.send(float(vec[1]))


def set_hs(vec):
Expand All @@ -179,8 +178,8 @@ def set_hs(vec):
_rp_oe.set(1)
_hs_oe.set(1)
_hp_oe.set(0)
_hs_sp_n.send(dumps(float(vec[0])))
_hs_sp_e.send(dumps(float(vec[1])))
_hs_sp_n.send(float(vec[0]))
_hs_sp_e.send(float(vec[1]))


def set_hp(vec):
Expand All @@ -189,8 +188,8 @@ def set_hp(vec):
_rp_oe.set(1)
_hs_oe.set(1)
_hp_oe.set(1)
_hp_sp_n.send(dumps(float(vec[0])))
_hp_sp_e.send(dumps(float(vec[1])))
_hp_sp_n.send(float(vec[0]))
_hp_sp_e.send(float(vec[1]))


if __name__ == '__main__':
Expand Down
7 changes: 3 additions & 4 deletions battmon/service/battmon.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@


from misc import Hysteresis
from msgpack import loads, dumps
from scl import scl_get_socket, SCL_Reader
from opcd_interface import OPCD_Interface
from misc import daemonize
Expand Down Expand Up @@ -60,14 +59,14 @@ def main(name):
low_cell_voltage_load = opcd.get('battmon.low_cell_voltage_load')
current_treshold = opcd.get('battmon.current_treshold')
vmax = cells * 4.1
voltage = loads(voltage_socket.recv())[0]
voltage = voltage_socket.recv()[0]
vmin_idle = cells * low_cell_voltage_idle
vmin_load = cells * low_cell_voltage_load
time_prev = time()
critical = False
while True:
current = current_reader.data[0]
voltage = loads(voltage_socket.recv())[0]
voltage = voltage_socket.recv()[0]
time_prev = time()
if current < current_treshold:
vmin = vmin_idle
Expand All @@ -80,7 +79,7 @@ def main(name):
if critical:
warning()
percent = min(1.0, max(0.0, (voltage - vmin) / (vmax - vmin)))
battery_socket.send(dumps((percent, critical)))
battery_socket.send([percent, critical])


daemonize('battmon', main)
Expand Down
18 changes: 7 additions & 11 deletions cmc/tools/cmc_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,16 @@
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details. """


from ctrl_api import *
from geomath import rad2deg
from numpy import array
import numpy as np
from scl import scl_get_socket, SCL_Reader
from opcd_interface import OPCD_Interface
from time import sleep
from threading import Thread
from msgpack import loads, dumps


thrust_socket = scl_get_socket('thrust', 'pub')
torques_socket = scl_get_socket('torques', 'pub')
mot_en_socket = scl_get_socket('mot_en', 'push')
mag = SCL_Reader('mag_adc_cal', 'sub', [0.0, 0.0, 0.0])
curr = SCL_Reader('current', 'sub', [0.0])
int_en = SCL_Reader('int_en', 'sub', 1)
Expand All @@ -53,10 +49,10 @@

# start motors:
sleep(1)
mot_en_socket.send(dumps(1))
mot_en(True)
for i in range(10000):
thrust_socket.send(dumps(0.0))
torques_socket.send(dumps([0.0, 0.0, 0.0]))
set_thrust(0.0)
set_torques([0.0, 0.0, 0.0])

# increase gas and store measurements:
o_start = orientation.data[0]
Expand All @@ -65,15 +61,15 @@
thrust = 0.0
while True:
thrust += 0.01
thrust_socket.send(dumps(thrust))
torques_socket.send(dumps([0.0, 0.0, 0.0]))
set_thrust(thrust)
set_torques([0.0, 0.0, 0.0])
meas.append([curr.data[0]] + mag.data)
sleep(0.01)
print 'error:', rad2deg(o_start - orientation.data[0])
#if not int_en.data:
# break
finally:
mot_en_socket.send(dumps(0))
mot_en(False)

opcd = OPCD_Interface()

Expand Down
6 changes: 3 additions & 3 deletions elevmap/service/elevmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
from scl import scl_get_socket
from misc import daemonize, RateTimer
from gps_msgpack import *
from msgpack import loads, dumps
from srtm import SrtmElevMap


Expand All @@ -39,9 +38,10 @@ def main(name):
elev_socket = scl_get_socket('elev', 'pub')
rt = RateTimer(2.0)
while True:
gps = loads(gps_socket.recv())
gps = gps_socket.recv()
if rt.expired():
elev = elev_map.lookup((gps[LON], gps[LAT]))
elev_socket.send(dumps(elev))
elev_socket.send(elev)


daemonize('elevmap', main)
7 changes: 3 additions & 4 deletions flight_detect/service/flight_detect.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,13 @@

from misc import daemonize
from scl import scl_get_socket
from msgpack import loads, dumps
from numpy import var


def main(name):
acc_socket = scl_get_socket('acc_neu', 'sub')
flying_socket = scl_get_socket('flying', 'pub')
acc_vec = loads(acc_socket.recv())
acc_vec = acc_socket.recv()

size = 80
histn = [acc_vec[0]] * size
Expand All @@ -44,7 +43,7 @@ def main(name):

i = 0
while True:
acc_vec = loads(acc_socket.recv())
acc_vec = acc_socket.recv()
i += 1
if i < 4:
continue
Expand All @@ -57,7 +56,7 @@ def main(name):
else:
s = 0
if s != sp:
flying_socket.send(dumps(s))
flying_socket.send(s)
sp = s

daemonize('flight_detect', main)
3 changes: 1 addition & 2 deletions flight_logic/manual_logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@

from sticks import *
from scl import scl_get_socket, SCL_Reader
from msgpack import dumps, loads
from time import sleep
from geomath import vec2_rot
from ctrl_api import *
Expand Down Expand Up @@ -59,7 +58,7 @@ def mot_en_cb(gesture):
hold_baro = None
pos_locked = None
while True:
rc_data = loads(rc_socket.recv())
rc_data = rc_socket.recv()
if rc_data[0]:
pitch_stick, roll_stick, yaw_stick, gas_stick, on_switch, mode_switch = rc_data[1:7]
mot_en(on_switch > 0.5)
Expand Down
6 changes: 2 additions & 4 deletions geomag/service/geomag.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
from time import time
from datetime import datetime
from gps_msgpack import *
from msgpack import loads, dumps
from math import pi


Expand All @@ -44,13 +43,12 @@ def main(name):
decl_socket = scl_get_socket('decl', 'pub')
rt = RateTimer(1)
while True:
raw = gps_socket.recv()
gps = gps_socket.recv()
if rt.expired():
gps = loads(raw)
try:
date = datetime.strptime(gps[TIME], '%Y-%m-%d %H:%M:%S').date()
decl = pi * gm.GeoMag(gps[LAT], gps[LON], time = date).dec / 180.0
decl_socket.send(dumps(decl))
decl_socket.send(decl)
except:
pass

Expand Down
10 changes: 3 additions & 7 deletions heartbeat/service/heartbeat.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
from scl import generate_map
from math import sin, cos, pi
from misc import daemonize, RateTimer
from msgpack import loads, dumps
from aircomm_shared import BCAST_NOFW, HEARTBEAT
from gps_msgpack import *

Expand All @@ -45,11 +44,8 @@
def gps_reader():
global gps
socket = socket_map['gps']
rt = RateTimer(1)
while True:
data = socket.recv()
if rt.expired():
gps = loads(data)
gps = socket.recv()


def cpu_reader():
Expand All @@ -67,7 +63,7 @@ def pm_reader():
s = socket_map['powerman']
global voltage, current, critical
while True:
_voltage, _current, _, critical = loads(s.recv())
_voltage, _current, _, critical = s.recv()
if voltage is None:
voltage = _voltage
else:
Expand Down Expand Up @@ -109,7 +105,7 @@ def main(name):
data += [gps[LAT], gps[LON]]
except:
pass
socket.send(dumps(data))
socket.send(data)
except Exception, e:
print e
sleep(1.0)
Expand Down
Loading

0 comments on commit c2104b3

Please sign in to comment.