Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python examples rework #27

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
33 changes: 23 additions & 10 deletions Python/ADC.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,30 @@
import sys, time
import sys, signal, time
import navio.Common.util

import navio.adc
import navio.util

navio.util.check_apm()
if navio.Common.util.get_navio_version() == "NAVIO2":
import navio.Navio2.ADC as ADC
else:
import navio.Navio.ADC as ADC

adc = navio.adc.ADC()
navio.Common.util.check_apm()


def signal_handler(signal, frame):
print "You pressed Ctrl+C!"
sys.exit(0)


signal.signal(signal.SIGINT, signal_handler)
# print 'Press Ctrl+C to exit'

adc = ADC()
results = [0] * adc.channel_count

while (True):
s = ''
for i in range (0, adc.channel_count):
while True:
s = ""
for i in range(0, adc.channel_count):
results[i] = adc.read(i)
s += 'A{0}: {1:6.4f}V '.format(i, results[i] / 1000)
print(s)
s += "A{0}: {1:6.4f}V ".format(i, results[i] / 1000)
print (s)
time.sleep(0.5)
105 changes: 62 additions & 43 deletions Python/AccelGyroMag.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,38 +26,51 @@
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
"""

import spidev
import time
import argparse
import argparse
import sys
import navio.mpu9250
import navio.util

navio.util.check_apm()
import navio.Common.mpu9250
import navio.Common.util
import navio.Navio2.lsm9ds1

parser = argparse.ArgumentParser()
parser.add_argument("-i", help = "Sensor selection: -i [sensor name]. Sensors names: mpu is MPU9250, lsm is LSM9DS1")
navio.Common.util.check_apm()

if len(sys.argv) == 1:
print "Enter parameter"
parser.print_help()
sys.exit(1)
elif len(sys.argv) == 2:
sys.exit("Enter sensor name: mpu or lsm")

args = parser.parse_args()
def get_inertial_sensor():

if args.i == 'mpu':
print "Selected: MPU9250"
imu = navio.mpu9250.MPU9250()
elif args.i == 'lsm':
print "Selected: LSM9DS1"
imu = navio.lsm9ds1.LSM9DS1()
else:
print "Wrong sensor name. Select: mpu or lsm"
sys.exit(1)
if navio.Common.util.get_navio_version() == "NAVIO2":

parser = argparse.ArgumentParser()
parser.add_argument(
"-i",
help="Sensor selection: -i [sensor name]. Sensors names: mpu is MPU9250, lsm is LSM9DS1",
)

if len(sys.argv) == 1:
print "Enter parameter"
parser.print_help()
sys.exit(1)
elif len(sys.argv) == 2:
sys.exit("Enter sensor name: mpu or lsm")

args = parser.parse_args()

if args.i == "mpu":
print "Selected: MPU9250"
return navio.Common.mpu9250.MPU9250()
elif args.i == "lsm":
print "Selected: LSM9DS1"
return navio.Navio2.lsm9ds1.LSM9DS1()
else:
print "Wrong sensor name. Select: mpu or lsm"
sys.exit(1)
else:
print "Selected: MPU9250"
return navio.Common.mpu9250.MPU9250()


imu = get_inertial_sensor()

if imu.testConnection():
print "Connection established: True"
Expand All @@ -69,23 +82,29 @@
time.sleep(1)

while True:
# imu.read_all()
# imu.read_gyro()
# imu.read_acc()
# imu.read_temp()
# imu.read_mag()

# print "Accelerometer: ", imu.accelerometer_data
# print "Gyroscope: ", imu.gyroscope_data
# print "Temperature: ", imu.temperature
# print "Magnetometer: ", imu.magnetometer_data

# time.sleep(0.1)

m9a, m9g, m9m = imu.getMotion9()

print "Acc:", "{:+7.3f}".format(m9a[0]), "{:+7.3f}".format(m9a[1]), "{:+7.3f}".format(m9a[2]),
print " Gyr:", "{:+8.3f}".format(m9g[0]), "{:+8.3f}".format(m9g[1]), "{:+8.3f}".format(m9g[2]),
print " Mag:", "{:+7.3f}".format(m9m[0]), "{:+7.3f}".format(m9m[1]), "{:+7.3f}".format(m9m[2])

time.sleep(0.5)
# imu.read_all()
# imu.read_gyro()
# imu.read_acc()
# imu.read_temp()
# imu.read_mag()

# print "Accelerometer: ", imu.accelerometer_data
# print "Gyroscope: ", imu.gyroscope_data
# print "Temperature: ", imu.temperature
# print "Magnetometer: ", imu.magnetometer_data

# time.sleep(0.1)

m9a, m9g, m9m = imu.getMotion9()

print "Acc:", "{:+7.3f}".format(m9a[0]), "{:+7.3f}".format(
m9a[1]
), "{:+7.3f}".format(m9a[2]),
print " Gyr:", "{:+8.3f}".format(m9g[0]), "{:+8.3f}".format(
m9g[1]
), "{:+8.3f}".format(m9g[2]),
print " Mag:", "{:+7.3f}".format(m9m[0]), "{:+7.3f}".format(
m9m[1]
), "{:+7.3f}".format(m9m[2])

time.sleep(0.5)
29 changes: 15 additions & 14 deletions Python/Barometer.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,26 @@

import time

import navio.ms5611
import navio.util
import navio.Common.util

navio.util.check_apm()
import navio.Common.ms5611

baro = navio.ms5611.MS5611()
navio.Common.util.check_apm()

baro = navio.Common.ms5611.MS5611()
baro.initialize()

while(True):
baro.refreshPressure()
time.sleep(0.01) # Waiting for pressure data ready 10ms
baro.readPressure()
while True:
baro.refreshPressure()
time.sleep(0.01) # Waiting for pressure data ready 10ms
baro.readPressure()

baro.refreshTemperature()
time.sleep(0.01) # Waiting for temperature data ready 10ms
baro.readTemperature()
baro.refreshTemperature()
time.sleep(0.01) # Waiting for temperature data ready 10ms
baro.readTemperature()

baro.calculatePressureAndTemperature()
baro.calculatePressureAndTemperature()

print "Temperature(C): %.6f" % (baro.TEMP), "Pressure(millibar): %.6f" % (baro.PRES)
print "Temperature(C): %.6f" % (baro.TEMP), "Pressure(millibar): %.6f" % (baro.PRES)

time.sleep(1)
time.sleep(1)
90 changes: 59 additions & 31 deletions Python/GPS.py
Original file line number Diff line number Diff line change
@@ -1,54 +1,82 @@
import navio.util
import navio.ublox

import navio.Common.ublox

if __name__ == "__main__":

ubl = navio.ublox.UBlox("spi:0.0", baudrate=5000000, timeout=2)
ubl = navio.Common.ublox.UBlox("spi:0.0", baudrate=5000000, timeout=2)

ubl.configure_poll_port()
ubl.configure_poll(navio.ublox.CLASS_CFG, navio.ublox.MSG_CFG_USB)
#ubl.configure_poll(navio.ublox.CLASS_MON, navio.ublox.MSG_MON_HW)
ubl.configure_poll(navio.Common.ublox.CLASS_CFG, navio.Common.ublox.MSG_CFG_USB)
# ubl.configure_poll(navio.ublox.CLASS_MON, navio.ublox.MSG_MON_HW)

ubl.configure_port(port=navio.ublox.PORT_SERIAL1, inMask=1, outMask=0)
ubl.configure_port(port=navio.ublox.PORT_USB, inMask=1, outMask=1)
ubl.configure_port(port=navio.ublox.PORT_SERIAL2, inMask=1, outMask=0)
ubl.configure_port(port=navio.Common.ublox.PORT_SERIAL1, inMask=1, outMask=0)
ubl.configure_port(port=navio.Common.ublox.PORT_USB, inMask=1, outMask=1)
ubl.configure_port(port=navio.Common.ublox.PORT_SERIAL2, inMask=1, outMask=0)
ubl.configure_poll_port()
ubl.configure_poll_port(navio.ublox.PORT_SERIAL1)
ubl.configure_poll_port(navio.ublox.PORT_SERIAL2)
ubl.configure_poll_port(navio.ublox.PORT_USB)
ubl.configure_poll_port(navio.Common.ublox.PORT_SERIAL1)
ubl.configure_poll_port(navio.Common.ublox.PORT_SERIAL2)
ubl.configure_poll_port(navio.Common.ublox.PORT_USB)
ubl.configure_solution_rate(rate_ms=1000)

ubl.set_preferred_dynamic_model(None)
ubl.set_preferred_usePPP(None)

ubl.configure_message_rate(navio.ublox.CLASS_NAV, navio.ublox.MSG_NAV_POSLLH, 1)
ubl.configure_message_rate(navio.ublox.CLASS_NAV, navio.ublox.MSG_NAV_PVT, 1)
ubl.configure_message_rate(navio.ublox.CLASS_NAV, navio.ublox.MSG_NAV_STATUS, 1)
ubl.configure_message_rate(navio.ublox.CLASS_NAV, navio.ublox.MSG_NAV_SOL, 1)
ubl.configure_message_rate(navio.ublox.CLASS_NAV, navio.ublox.MSG_NAV_VELNED, 1)
ubl.configure_message_rate(navio.ublox.CLASS_NAV, navio.ublox.MSG_NAV_SVINFO, 1)
ubl.configure_message_rate(navio.ublox.CLASS_NAV, navio.ublox.MSG_NAV_VELECEF, 1)
ubl.configure_message_rate(navio.ublox.CLASS_NAV, navio.ublox.MSG_NAV_POSECEF, 1)
ubl.configure_message_rate(navio.ublox.CLASS_RXM, navio.ublox.MSG_RXM_RAW, 1)
ubl.configure_message_rate(navio.ublox.CLASS_RXM, navio.ublox.MSG_RXM_SFRB, 1)
ubl.configure_message_rate(navio.ublox.CLASS_RXM, navio.ublox.MSG_RXM_SVSI, 1)
ubl.configure_message_rate(navio.ublox.CLASS_RXM, navio.ublox.MSG_RXM_ALM, 1)
ubl.configure_message_rate(navio.ublox.CLASS_RXM, navio.ublox.MSG_RXM_EPH, 1)
ubl.configure_message_rate(navio.ublox.CLASS_NAV, navio.ublox.MSG_NAV_TIMEGPS, 5)
ubl.configure_message_rate(navio.ublox.CLASS_NAV, navio.ublox.MSG_NAV_CLOCK, 5)
#ubl.configure_message_rate(navio.ublox.CLASS_NAV, navio.ublox.MSG_NAV_DGPS, 5)
ubl.configure_message_rate(
navio.Common.ublox.CLASS_NAV, navio.Common.ublox.MSG_NAV_POSLLH, 1
)
ubl.configure_message_rate(
navio.Common.ublox.CLASS_NAV, navio.Common.ublox.MSG_NAV_PVT, 1
)
ubl.configure_message_rate(
navio.Common.ublox.CLASS_NAV, navio.Common.ublox.MSG_NAV_STATUS, 1
)
ubl.configure_message_rate(
navio.Common.ublox.CLASS_NAV, navio.Common.ublox.MSG_NAV_SOL, 1
)
ubl.configure_message_rate(
navio.Common.ublox.CLASS_NAV, navio.Common.ublox.MSG_NAV_VELNED, 1
)
ubl.configure_message_rate(
navio.Common.ublox.CLASS_NAV, navio.Common.ublox.MSG_NAV_SVINFO, 1
)
ubl.configure_message_rate(
navio.Common.ublox.CLASS_NAV, navio.Common.ublox.MSG_NAV_VELECEF, 1
)
ubl.configure_message_rate(
navio.Common.ublox.CLASS_NAV, navio.Common.ublox.MSG_NAV_POSECEF, 1
)
ubl.configure_message_rate(
navio.Common.ublox.CLASS_RXM, navio.Common.ublox.MSG_RXM_RAW, 1
)
ubl.configure_message_rate(
navio.Common.ublox.CLASS_RXM, navio.Common.ublox.MSG_RXM_SFRB, 1
)
ubl.configure_message_rate(
navio.Common.ublox.CLASS_RXM, navio.Common.ublox.MSG_RXM_SVSI, 1
)
ubl.configure_message_rate(
navio.Common.ublox.CLASS_RXM, navio.Common.ublox.MSG_RXM_ALM, 1
)
ubl.configure_message_rate(
navio.Common.ublox.CLASS_RXM, navio.Common.ublox.MSG_RXM_EPH, 1
)
ubl.configure_message_rate(
navio.Common.ublox.CLASS_NAV, navio.Common.ublox.MSG_NAV_TIMEGPS, 5
)
ubl.configure_message_rate(
navio.Common.ublox.CLASS_NAV, navio.Common.ublox.MSG_NAV_CLOCK, 5
)
# ubl.configure_message_rate(navio.ublox.CLASS_NAV, navio.ublox.MSG_NAV_DGPS, 5)

while True:
msg = ubl.receive_message()
if msg is None:
if opts.reopen:
ubl.close()
ubl = navio.ublox.UBlox("spi:0.0", baudrate=5000000, timeout=2)
ubl = navio.Common.ublox.UBlox("spi:0.0", baudrate=5000000, timeout=2)
continue
print(empty)
break
#print(msg.name())
# print(msg.name())
if msg.name() == "NAV_POSLLH":
outstr = str(msg).split(",")[1:]
outstr = "".join(outstr)
Expand All @@ -57,4 +85,4 @@
outstr = str(msg).split(",")[1:2]
outstr = "".join(outstr)
print(outstr)
#print(str(msg))
# print(str(msg))
37 changes: 23 additions & 14 deletions Python/LED.py
Original file line number Diff line number Diff line change
@@ -1,39 +1,48 @@
import sys

import navio.leds
import time
import navio.util
import os

import navio.Common.util

if navio.Common.util.get_navio_version() == "NAVIO2":
import navio.Navio2.Led as Led
else:
import navio.Navio.Led as Led


if os.getuid() != 0:
print "Not root. Please, launch like this: sudo python LED.py"
exit(-1)

navio.util.check_apm()
navio.Common.util.check_apm()

led = navio.leds.Led()
led = Led()

led.setColor('Yellow')
led.setColor("Yellow")
print "LED is yellow"
time.sleep(1)

while (True):
while True:

led.setColor('Green')
led.setColor("Green")
print "LED is green"
time.sleep(1)

led.setColor('Cyan')
led.setColor("Cyan")
print "LED is cyan"
time.sleep(1)

led.setColor('Blue')
led.setColor("Blue")
print "LED is blue"
time.sleep(1)

led.setColor('Magenta')
led.setColor("Magenta")
print "LED is magenta"
time.sleep(1)

led.setColor('Red')
led.setColor("Red")
print "LED is red"
time.sleep(1)

led.setColor('Yellow')
led.setColor("Yellow")
print "LED is yellow"
time.sleep(1)
Loading