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

referencing hardware outputs

Wed Jun 25, 2014 6:16 pm

hi all,
as i write a code for my raspberry pi, how would i go about calling the outputs from my hardware? I'm creating a robotics project essentially. using a GPS, gyro and compass/accelerometer simultaneously. i imagine it's a reference or input command? maybe something like: from gps use lat,long; from gyro use x,y,z so on and so forth... apologies if this is more of a python question

User avatar
FLYFISH TECHNOLOGIES
Posts: 1750
Joined: Thu Oct 03, 2013 7:48 am
Location: Ljubljana, Slovenia
Contact: Website

Re: referencing hardware outputs

Wed Jun 25, 2014 6:51 pm

Hi,
mighty69 wrote:how would i go about calling the outputs from my hardware?
Use short descriptive names. (1st priority is to be descriptive, 2nd is to make it short).

I'd use few characters describing sensor type (position, orientation, acceleration...) and a name itself, like: PosLat, PosLong, AccX, MagY, RollZ, etc...

According to the variables used in a code, naming is typically specified in a Coding style document, which each (serious) company has at least one for each programming language used. Also some vendors (eg. of software tools, like compilers) can also publish their recommendations.
You can google a bit to take a look how some others defined naming conventions to compose variables names.
My default selection is 3 characters describing variable type + camel-case "description" (eg. strServerName, intMemberIndex).


Best wishes, Ivan Zilic.
Running out of GPIO pins and/or need to read analog values?
Solution: http://www.flyfish-tech.com/FF32

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

Re: referencing hardware outputs

Wed Jun 25, 2014 10:06 pm

Excellent, thank you Ivan.
Essentially the naming is arbitrary then? As far as coding is concerned then, I would just use the name and assign it to the hardware somehow? I'm failing to see how the program knows to access the hardware for providing data. Reference the i2c address? or GPIO? I feel like I can write code all day and never get what I need.

User avatar
Douglas6
Posts: 4874
Joined: Sat Mar 16, 2013 5:34 am
Location: Chicago, IL

Re: referencing hardware outputs

Wed Jun 25, 2014 11:02 pm

The ways you will access hardware devices will depend on the device manufacturer and model. Typically, you'll use a Python module written specifically for that particular hardware or protocol. Pick a device to start with, say an accelerometer, and Google the part number to find example Python code. A seller like Adafruit in the US will have online tutorials and sample code for the products they sell.

User avatar
FLYFISH TECHNOLOGIES
Posts: 1750
Joined: Thu Oct 03, 2013 7:48 am
Location: Ljubljana, Slovenia
Contact: Website

Re: referencing hardware outputs

Wed Jun 25, 2014 11:58 pm

Hi,
mighty69 wrote:Essentially the naming is arbitrary then?
For the parts you implement you're free to use naming you like... for using already implemented bits use names they picked.
mighty69 wrote:I would just use the name and assign it to the hardware somehow?
This your "assign" task is actually reading from/writing to hardware.
mighty69 wrote:I'm failing to see how the program knows to access the hardware for providing data.
Each component has a datasheet where this is defined. Some standard ways exist, named as protocols (I2C, SPI, ...).
Good reference are samples others have published (what Douglas already mentioned).
mighty69 wrote:I feel like I can write code all day and never get what I need.
Coding typically takes minor amount of overall effort. Initially, reading and learning takes most of this time.
Start coding when you'll know what to code. Then you're going to deal just with how to code...


Best wishes, Ivan Zilic.
Running out of GPIO pins and/or need to read analog values?
Solution: http://www.flyfish-tech.com/FF32

User avatar
Douglas6
Posts: 4874
Joined: Sat Mar 16, 2013 5:34 am
Location: Chicago, IL

Re: referencing hardware outputs

Thu Jun 26, 2014 12:23 am

FLYFISH TECHNOLOGIES wrote:Coding typically takes minor amount of overall effort. Initially, reading and learning takes most of this time.
And troubleshooting, debugging and finding your own mistakes takes the other 90% of your time. But I've noticed in my career, as computers have evolved from room-sized, to refrigerator-sized, to breadbox-sized, to "credit card sized", the smaller they get, the more motivated I am to "best" them.

ame
Posts: 3172
Joined: Sat Aug 18, 2012 1:21 am
Location: New Zealand

Re: referencing hardware outputs

Thu Jun 26, 2014 1:27 am

mighty69 wrote:I'm failing to see how the program knows to access the hardware for providing data.
Umm, it doesn't.

You know how to access the hardware and get the data, so it's your job to embed that knowledge in the program you are writing.

There might be some pre-written modules/templates/utilities that will do some of the work for you, but it's your job to find them, evaluate them, incorporate them, or write your own.

As a programmer you might throw up your hands in frustration and ask "Do I have to think of everything?!". The answer is "Yes", every tiny detail.

User avatar
AndrewS
Posts: 3625
Joined: Sun Apr 22, 2012 4:50 pm
Location: Cambridge, UK
Contact: Website

Re: referencing hardware outputs

Thu Jun 26, 2014 2:09 am

ame wrote:As a programmer you might throw up your hands in frustration and ask "Do I have to think of everything?!". The answer is "Yes", every tiny detail.
But it's much easier to program in a high-level language like Python, than a low-level language like assembler :-)
(you have to consider many more details when using assembler)

/me prepares his asbestos overcoat ;)

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

Re: referencing hardware outputs

Thu Jun 26, 2014 4:00 pm

I really appreciate the help.
Let me see if I understand completely:

I'm using the LSM303DLHC Compass+accel from adafruit and I've looked through the tutorial and found/downloaded the module to use. Adafruit.com supplies everything for Arduino in C++ looks like. But I found this:

https://bitbucket.org/shigeru_kawaguchi ... ?at=master

So I download this source code and import it into mine just like I would import sys, math or other modules?
Then when I need data (e.g. acceleration in x direction), I write accelData.x into my equations? or set it equal to a variable and use the variable?

User avatar
AndrewS
Posts: 3625
Joined: Sun Apr 22, 2012 4:50 pm
Location: Cambridge, UK
Contact: Website

Re: referencing hardware outputs

Fri Jun 27, 2014 3:06 am

That script includes a handy test section...
Take a look at line 381, line 394 and line 398 to see how to access the acceleration data.
(But yes, if using this in your own code you'd need to "from Adafruit_LSM303DLHC import LSM303DLHC")

Oh, and have a look at https://bitbucket.org/shigeru_kawaguchi ... ?at=master as well :roll:

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

Re: referencing hardware outputs

Mon Jun 30, 2014 2:29 pm

If I call the module alone it works just fine, but when I import it into my code and try to run my program I get the following error message:

Code: Select all

Traceback (most recent call last)"
    File "Master.py", line 4, in <module>
        from Adafruit_I2C import Adafruit_I2C
    File "/home/pi/Adafruit_I2C.py", line 23, in <module>
        class Adafruit_I2C:
    File "/home/pi/Adafruit_I2C.py", line 37, in Adafruit_I2C
        def _init_(self, address, bus=smbus.SMBus (1 if getPiREvision() >1 else 0), debug=False)
IOError: [Errno 13] Permission denied
I haven't changed anything in the modules I'm calling, only calling them in my program. I can run the test program mentioned earlier just fine, and all the other modules that reference the Adafruit_I2C run fine.

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

Re: referencing hardware outputs

Mon Jun 30, 2014 2:34 pm

the lines in question in my code are:

Code: Select all

from Adafruit_I2C import Adafruit_I2C
from Adafruit_L3GD20 import L3GD20
from Adafruit_LSM303DLHC import LSM303DLHC
if I comment out the lines, my program runs as it did previously.

User avatar
DougieLawson
Posts: 39301
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: referencing hardware outputs

Mon Jun 30, 2014 2:38 pm

Does Rasbian have the i2c modules loaded? Or are they still blacklisted?

Does that Adafruit stuff have to run with uid(0)? Does it work with sudo python foo.py?
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

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

Re: referencing hardware outputs

Mon Jun 30, 2014 2:42 pm

I commented the i2c blacklist out, so it should work. never seen foo.py though?

User avatar
rpdom
Posts: 17275
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: referencing hardware outputs

Mon Jun 30, 2014 2:54 pm

mighty69 wrote:I commented the i2c blacklist out, so it should work. never seen foo.py though?
Hehe!

"Foo" is a generic term used for "insert the name of your stuff here".

(See http://en.wikipedia.org/wiki/Foo),

So when you are asked if you tried "sudo python foo.py", what is meant is have you tried "sudo python your_program_name.py" :)

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

Re: referencing hardware outputs

Mon Jun 30, 2014 3:11 pm

ahhhhhh I see.
This is my first exposure to any sort of coding or programming so I apologize retrospectively.
My first thoughts were, what is this "foo", and how do I get it?
regardless,
I haven't changed anything, but now it runs. Seems to happen a lot, and I can't really explain it.
sudo python MasterProgram.py runs how it did before I imported anything. Now I need to make some progress.

from MasterProgram.py:

Code: Select all

#calculate position B using accelerations
def findB(t,p1,p2,p3):
    return array([2*t**2+p1, 3*t**2+p2, 9.8*t**2+p3])
I have a simple equation for position based on time, acceleration and initial position, nothing too fancy. what I need to do is use the values of acceleration from the hardware instead of the arbitrary values I made up (2,3,9.8) and similarly use the gps location instead of the arbitrary position coordinates (p1,p2,p3).
shortcuts and advice welcome.

User avatar
AndrewS
Posts: 3625
Joined: Sun Apr 22, 2012 4:50 pm
Location: Cambridge, UK
Contact: Website

Re: referencing hardware outputs

Mon Jun 30, 2014 6:32 pm

Code: Select all

#calculate position B using accelerations
def findB(t, pos, accel):
    tsq = t**2
    return array([accel[0]*tsq+pos[0], accel[1]*tsq+pos[1], accel[2]*tsq+pos[2]])
Sounds to me like you really need to try reading a few python tutorials http://www.raspberrypi.org/documentatio ... /README.md

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

Re: referencing hardware outputs

Mon Jun 30, 2014 7:25 pm

Yup, read that one, read a bunch of others. all very informative and answered none of the questions I've posted in this forum. great, i can find out how many letters are in my name or iterate a list of numbers from one - 100. doesn't really help with the advanced robotics i'm trying to get a handle on here. haven't found a tutorial that explains calling or importing modules into another code.


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

Re: referencing hardware outputs

Tue Jul 01, 2014 8:12 pm

Thanks to everyone once again for the help with this project, especially Andrew S.
I'm so close to getting this and then my project will take off.
the short version is this:

Code: Select all

from Adafruit_LSM303DLHC import LSM303DLHC
lsm = LSM303DLHC(0x19, 0x1E, False)
accel = lsm.readAccelerationsG()
from my understanding, that will give me the function readAccelerationsG from class LSM303DLHC in module Adafruit_LSM303DLHC
that function returns accelVal3D, which is a list of the accelerations in the three directions. In the test module he simply prints these three values using

Code: Select all

print "Accel X: %6.3f G, Accel Y: %6.3f G, Accel Z: %6.3f G" % (accel.x, accel.y, accel.z)
To use those values in my own function is where i'm still tripping up. when Andrew S. wrote

Code: Select all

#calculate position B using accelerations
def findB(t, pos, accel):
    tsq = t**2
    return array([accel[0]*tsq+pos[0], accel[1]*tsq+pos[1], accel[2]*tsq+pos[2]])
those three imputs are an integer, "t" and two arrays, "pos" and "accel" and the function refers to the positions in the arrays? so accel[0]=accel.x?

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

Re: referencing hardware outputs

Tue Jul 01, 2014 9:11 pm

Here is the portion of my project i've been asking about. feel free to cut me to pieces; like i said i've never seen python or an RPi before a few weeks ago.

Code: Select all

import os
import math
from Adafruit_I2C import Adafruit_I2C
from Adafruit_LSM303DLHC import LSM303DLHC
import numpy as np
from numpy import *
import time
from datetime import datetime, date

lsm = LSM303DLHC (0x19, 0x1E, False)
lsm.setTempEnabled (True)

def sub(a, b)
    return array([b-a])

def findB(t, p, a):
    tsq=t**2
    return array([a[0]*tsq+p[0], a[1]*tsq+p[1], a[2]*tsq+p[2]])

while (1):
    accell= lsm.readAccelerationsG()
    temp = lsm.readTempF()
    heading = lsm.readMagneticHeading()

    def main():
        pos1=input()
        pos2=input()
        pos3=input()
        pos5=input()
        pos4=input()
        pos6=0
        time7=input()
        start=array([pos4, pos5, pos6])
        finish=array([pos1, pos2, pos3])
        current=findB(time7, start, accel)
        print(sub(start, finish))
        if time7>0:
            print(findB(time7, current, accel))
        print(sub(current, finish))
    main()

User avatar
AndrewS
Posts: 3625
Joined: Sun Apr 22, 2012 4:50 pm
Location: Cambridge, UK
Contact: Website

Re: referencing hardware outputs

Tue Jul 01, 2014 9:56 pm

mighty69 wrote:these three values using

Code: Select all

print "Accel X: %6.3f G, Accel Y: %6.3f G, Accel Z: %6.3f G" % (accel.x, accel.y, accel.z)
To use those values in my own function is where i'm still tripping up. when Andrew S. wrote

Code: Select all

#calculate position B using accelerations
def findB(t, pos, accel):
    tsq = t**2
    return array([accel[0]*tsq+pos[0], accel[1]*tsq+pos[1], accel[2]*tsq+pos[2]])
those three imputs are an integer, "t" and two arrays, "pos" and "accel" and the function refers to the positions in the arrays? so accel[0]=accel.x?
Yeah, you could simply change the findB function so that it takes accel as an object parameter, rather than as an array:

Code: Select all

#calculate position B using accelerations
def findB(t, pos, accel):
    tsq = t**2
    return array([accel.x*tsq+pos[0], accel.y*tsq+pos[1], accel.z*tsq+pos[2]])

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

Re: referencing hardware outputs

Wed Jul 02, 2014 1:36 pm

I get an AttributeError: 'function' object has no attribute 'x' when I use the format

Code: Select all

accel.x*tsq+p[0]
when I use

Code: Select all

accel[0]*tsq+p[0]
I get a different AttributeError: Obj3D has no attribute '_getitem_'

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

Re: referencing hardware outputs

Wed Jul 02, 2014 4:01 pm

i got it working. needed to define the class Obj3D.

User avatar
AndrewS
Posts: 3625
Joined: Sun Apr 22, 2012 4:50 pm
Location: Cambridge, UK
Contact: Website

Re: referencing hardware outputs

Thu Jul 03, 2014 12:33 am

An error like "AttributeError: 'function' object has no attribute 'x'" typically indicates that you're doing "accell= lsm.readAccelerationsG" instead of "accell= lsm.readAccelerationsG()".

Return to “Beginners”