mighty69
Posts: 25
Joined: Tue Jun 03, 2014 2:10 pm

real-time data

Wed Aug 13, 2014 3:39 pm

I'm using a gyroscope for a robotics project. I found a library that generates a sensor output in degrees per second with an included test script. if i run the test script i can get real-time data from the sensor, but when i import the library into my code, i can only seem to get an initial read. Then as my function iterates, it simply repeats the initial value. I'd like to be able see real-time change similar to the test script. suggestions?

User avatar
joan
Posts: 14960
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: real-time data

Wed Aug 13, 2014 3:41 pm

Examine the test script to see what you are doing wrong.

mighty69
Posts: 25
Joined: Tue Jun 03, 2014 2:10 pm

Re: real-time data

Wed Aug 13, 2014 3:55 pm

i'm pretty new to python and programming. I thought that's what i had done, but i guess I'm not quite there.

mighty69
Posts: 25
Joined: Tue Jun 03, 2014 2:10 pm

Re: real-time data

Wed Aug 13, 2014 6:09 pm

when i run the test program without moving my model I get the same data results that my program generates. The difference is that when i move the model around while running the test program the results change, but this is not the case during my program run. I am mistaken in calling it a test program; it's actually and included test class.

mighty69
Posts: 25
Joined: Tue Jun 03, 2014 2:10 pm

Re: real-time data

Wed Aug 13, 2014 6:17 pm

I was able to confirm that the output datatype is, in fact, a float type in both cases, but for some reason i'm still having trouble. I essentially copy and pasted the code from the test class. still not sure what the issue is.

User avatar
joan
Posts: 14960
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: real-time data

Wed Aug 13, 2014 6:51 pm

Without visibility of the code it's going to be hard to make useful suggestions.

mighty69
Posts: 25
Joined: Tue Jun 03, 2014 2:10 pm

Re: real-time data

Wed Aug 13, 2014 8:49 pm

My code is here,
Lines 22-25 are the initial call and 75 -78 i think are the references i'm talking about:

Code: Select all

import os
import math
from Adafruit_I2C import Adafruit_I2C
from Adafruit_L3GD20 import *
from Adafruit_LSM303DLHC import *
from Adafruit_PWM_Servo_Driver import PWM
from servo_actuator import ServoActuator
import scipy
import numpy as np
from numpy import *
import time
from datetime import datetime, date, timedelta
#import gpsdata as gps

#accelerometer
lsm = LSM303DLHC(0x19, 0x1E, False)
lsm.setTempEnabled(True)
acc = lsm.readAccelerationsG()
heading = lsm.readMagneticHeading()

#gyroscope
l3g = L3GD20(0x6b, False)
gyro = l3g.readDps()

#servo motors
pwm = PWM(0x40, debug=True)
pwm.setPWMFreq(60)

def sub(a,b):
    return b-a
vecsub = vectorize(sub)

def AccelInput(accel,t):
	tsq = t**2
	G = 9.8
	return (G*accel*tsq)/2
	
def Velocity(a,t,v):
	G = 9.8
	return (G*a*t)+v
	
def prevelocity(p1,p2,t):
	if t < 2:
		return 0.0
	else:
		return (p2-p1)/t

def AngleRads(t,gyr):
	convert = t*math.pi/180
	return gyr*convert

def TerminalV(t,a,v):
	G = 9.8
	return t*(a*G)+v

count = 0
DeltaT = 1

class InitialCondition:
	#From GPS, find initial position (Start/Launch).  GPS output type = decimal coordinates.
	"""pos1 = gps.lat
	pos2 = gps.long
	pos3 = 0.0"""
	x=y=z = 0.0
	print('DESTINATION:')
	print("x'=10.00, y'=7.50, z'=16.09")
	pos4=10.00
	pos5=7.50
	pos6=16.09
	print('START (T=0):')
	print('x=0, y=0, z=0')
	boat = array([x, y, z])
	destination = array([pos4, pos5, pos6])
	Roll = gyro.x
	Pitch = gyro.y
	Yaw = gyro.z
	print'Launch orientation is:', (Roll, Pitch, Yaw)
	print'Desired impact orientation: (5, 6, 7)'
	launch = array([Roll, Pitch, Yaw])
	impact = array([5, 6, 7])
	print'Vector from Launch to Destination is:', vecsub(boat, destination)
	print'Rotation from Launch to Impact is:', vecsub(launch, impact)
	print("Ready to launch.")
	print'Heading:', heading

count = count +1
IC = InitialCondition
timelapsed = float(DeltaT * count)

class CurrentCondition:
	if timelapsed == DeltaT:
		previousx = IC.x
		previousy = IC.y
		previousz = IC.z
		currentx = previousx + AccelInput(acc.x, timelapsed)
		currenty = previousy + AccelInput(acc.y, timelapsed)
		elevation = previousz + AccelInput(acc.z, timelapsed)
		previousx = currentx
		previousy = currenty
		previousz = elevation	
		position = array([currentx, currenty, elevation])
	else:
		currentx = IC.x + (previousx - IC.x) + AccelInput(acc.x, timelapsed)
		currenty = IC.y + (previousy - IC.y) + AccelInput(acc.y, timelapsed)
		elevation = IC.z + (previousz - IC.z) + AccelInput(acc.z, timelapsed)
		previousx = currentx
		previousy = currenty
		previousz = elevation
		position = array([currentx, currenty, elevation])
		
count = count + 1
CC = CurrentCondition

User avatar
joan
Posts: 14960
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: real-time data

Wed Aug 13, 2014 9:05 pm

The line

gyro = l3g.readDps()

reads the gyro device and assigns the reading to a variable called gyro. Subsequently you refer to the variable (gyro) without re-reading the device. You need to repeat the line gyro = l3g.readDps() when you want a new reading.

mighty69
Posts: 25
Joined: Tue Jun 03, 2014 2:10 pm

Re: real-time data

Wed Aug 13, 2014 9:20 pm

That's what i've gathered from others as well. I put that line in the InitialCondition class and then again in the CurrentCondition class which is the one that repeats, but i'm still running into the same wall.

Code: Select all

import os
import math
from Adafruit_I2C import Adafruit_I2C
from Adafruit_L3GD20 import *
from Adafruit_LSM303DLHC import *
from Adafruit_PWM_Servo_Driver import PWM
from servo_actuator import ServoActuator
import scipy
import numpy as np
from numpy import *
import time
from datetime import datetime, date, timedelta
#import gpsdata as gps



#servo motors
pwm = PWM(0x40, debug=True)
pwm.setPWMFreq(60)

def sub(a,b):
    return b-a
vecsub = vectorize(sub)

def AccelInput(accel,t):
	tsq = t**2
	G = 9.8
	return (G*accel*tsq)/2
	
def Velocity(a,t,v):
	G = 9.8
	return (G*a*t)+v
	
def prevelocity(p1,p2,t):
	if t < 2:
		return 0.0
	else:
		return (p2-p1)/t

def AngleRads(t,gyr):
	convert = t*math.pi/180
	return gyr*convert

def TerminalV(t,a,v):
	G = 9.8
	return t*(a*G)+v


count = 0
DeltaT = 1

class InitialCondition:
	#accelerometer
	lsm = LSM303DLHC(0x19, 0x1E, False)
	lsm.setTempEnabled(True)
	acc = lsm.readAccelerationsG()
	heading = lsm.readMagneticHeading()
	#gyroscope
	l3g = L3GD20(0x6b, False)
	gyro = l3g.readDps()
	
	#From GPS, find initial position (Start/Launch).  GPS output type = decimal coordinates.
	"""pos1 = gps.lat
	pos2 = gps.long
	pos3 = 0.0"""
	x=y=z = 0.0
	print('DESTINATION:')
	print("x'=10.00, y'=7.50, z'=16.09")
	pos4=10.00
	pos5=7.50
	pos6=16.09
	print('START (T=0):')
	print('x=0, y=0, z=0')
	boat = array([x, y, z])
	destination = array([pos4, pos5, pos6])
	Roll = gyro.x
	Pitch = gyro.y
	Yaw = gyro.z
	print'Launch orientation is:', (Roll, Pitch, Yaw)
	print'Desired impact orientation: (5, 6, 7)'
	launch = array([Roll, Pitch, Yaw])
	impact = array([5, 6, 7])
	print'Vector from Launch to Destination is:', vecsub(boat, destination)
	print'Rotation from Launch to Impact is:', vecsub(launch, impact)
	print("Ready to launch.")
	print'Heading:', heading

count = count +1
IC = InitialCondition
timelapsed = float(DeltaT * count)

class CurrentCondition:
	#accelerometer
	lsm = LSM303DLHC(0x19, 0x1E, False)
	lsm.setTempEnabled(True)
	acc = lsm.readAccelerationsG()
	heading = lsm.readMagneticHeading()
	#gyroscope
	l3g = L3GD20(0x6b, False)
	gyro = l3g.readDps()
	
	if timelapsed == DeltaT:
		previousx = IC.x
		previousy = IC.y
		previousz = IC.z
		currentx = previousx + AccelInput(acc.x, timelapsed)
		currenty = previousy + AccelInput(acc.y, timelapsed)
		elevation = previousz + AccelInput(acc.z, timelapsed)
		previousx = currentx
		previousy = currenty
		previousz = elevation	
		position = array([currentx, currenty, elevation])
		count = count + 1
	else:
		currentx = IC.x + (previousx - IC.x) + AccelInput(acc.x, timelapsed)
		currenty = IC.y + (previousy - IC.y) + AccelInput(acc.y, timelapsed)
		elevation = IC.z + (previousz - IC.z) + AccelInput(acc.z, timelapsed)
		previousx = currentx
		previousy = currenty
		previousz = elevation
		position = array([currentx, currenty, elevation])
		count = count + 1
CC = CurrentCondition


class TransitAnalysis:
   print("Launch.")
	#gyroscope
	l3g = L3GD20(0x6b, False)
	gyro = l3g.readDps()
	while (True):
		time.sleep(1)
		if CC.acc.x < 0.1:
			CC.acc.x = 0.000
		if CC.acc.y < 0.1:
			CC.acc.y = 0.000
	
		"""if gyro.x - IC.Roll > 0:
			VX = math.cos(AngleRads(DeltaT, gyro.y)) * (Velocity(acc.x,DeltaT,vx))
		
	
		if gyro.y - IC.Pitch > 0:
			VY = math.cos(AngleRads(DeltaT, gyro.y)) * (Velocity(acc.y,DeltaT,vy))
		
	
		if gyro.z - IC.Yaw > 0:
			FallVel = math.cos(AngleRads(DeltaT, gyro.z)) *(Vprev(z, previousz, timelapsed))
		
		
		currentx = AccPos(acc.x, DeltaT, VX, previousx)
		currenty = AccPos(acc.y, DeltaT, VY, previousy)
		elevation = Vprev(DeltaT, previousz, pos3) + previousz"""
	
		CurrentRoll = AngleRads(DeltaT, CC.gyro.x)
		CurrentPitch = AngleRads(DeltaT, CC.gyro.y)
		CurrentYaw = AngleRads(DeltaT, CC.gyro.z)
	
		
		CurrentO = array([CC.gyro.x, CC.gyro.y, CC.gyro.z])
		
		print'Position:', CC.position
		print'Vector from Position to Destination is:', vecsub(CC.position, IC.destination)
		print'Current orientation, Gyro X: %8.3f dps Y: %8.3f dps Z: %8.3f dps' % (CC.gyro.x, CC.gyro.y, CC.gyro.z)
		print'Rotation from Current to Impact is:', vecsub(CurrentO, IC.impact)
		print'Heading:', CC.heading
	
		"""if TerminalV(DeltaT, CC.acc.z, FallVel) > 19.5:
			print "That's all she's got Captain."	"""
I'm guessing i have to call it during the TransitAnalysis class as well?

Return to “General discussion”