STAccel script in STM32F4 DISC

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
gk26
Posts: 2
Joined: Thu Mar 12, 2015 10:14 pm

STAccel script in STM32F4 DISC

Post by gk26 » Thu Mar 12, 2015 10:17 pm

Hello Guys,
I am new to Python and come from embedded software. I compiled firmware for discovery board, flashed it using STLink, and was able to get some LED's blinking with a script.
I am excited how quick it is to use compared to traditional firmware development.
Now, I am trying to run staccel.py . I copied it into PYBFLASH.
I did import staccel in repl, but when I try to create a object using 'a = staccel.STAccel()', it gives me error saying, module object has no attribute STAccel. I am confused as the script definitely has STAccel. What am I missing here?

billybumbler
Posts: 4
Joined: Sat Nov 08, 2014 1:44 am

Re: STAccel script in STM32F4 DISC

Post by billybumbler » Wed Mar 25, 2015 12:51 am

I was just looking at the source and noticed that the STAccel class has a syntax error that may be causing the error. There are no () in the class definition of STAccel.

Damien
Site Admin
Posts: 647
Joined: Mon Dec 09, 2013 5:02 pm

Re: STAccel script in STM32F4 DISC

Post by Damien » Wed Mar 25, 2015 11:20 pm

It's not a syntax error and the import and creation of the object should work:

Code: Select all

>>> import staccel
>>> ac = staccel.STAccel()
Although for me it gives another error: Exception: LIS302DL or LIS3DSH accelerometer not present...

billybumbler
Posts: 4
Joined: Sat Nov 08, 2014 1:44 am

Re: STAccel script in STM32F4 DISC

Post by billybumbler » Thu Mar 26, 2015 10:44 am

You're right Damien, sorry about that. :oops: It seemed like all the materials I read showed the parentheses, and I just assumed they were required.

I have the discovery board with MicroPython and the demo that uses the accelerometer to turn on LEDs based on the direction of tilt worked fine for me. It was at least a couple months ago that I tried it though.



Edit: It was November actually

SpotlightKid
Posts: 463
Joined: Wed Apr 08, 2015 5:19 am

Re: STAccel script in STM32F4 DISC

Post by SpotlightKid » Wed Apr 08, 2015 6:06 am

Damien wrote:Although for me it gives another error: Exception: LIS302DL or LIS3DSH accelerometer not present...
I just tested this myself, git the same error and tracked it down.

It seems that the call to STAccel.read_id() in STAccel.__init__() always returns 255 instead of the correct value 63. In fact, the first read on the SPI bus after creating the pyb.SPI instance seems to always return 255.

So I just fixed this by reading the ID again:

Code: Select all

class STAccel:
    def __init__(self, cs='PE3', spi=1, debug=False):
        [...]
        self.read_id()
        # First SPI read always returns 255 --> discard and read ID again
        self.who_am_i = self.read_id()
and then everything works as it should.

SpotlightKid
Posts: 463
Joined: Wed Apr 08, 2015 5:19 am

Re: STAccel script in STM32F4 DISC

Post by SpotlightKid » Wed Apr 08, 2015 4:07 pm

And here's an adapted version of the USB-HID accelerometer-mouse tutorial:

Code: Select all

# -*- coding: utf-8 -*-
"""Move mouse pointer via USB-HID according to accelerometer measurements."""

import pyb
from staccel import STAccel


FREQ = const(50)
MAG = const(30)

def set_sw_state():
    global sw_state
    sw_state = not sw_state
    led.toggle()

sw_state = False
led = pyb.LED(1)
switch = pyb.Switch()
switch.callback(set_sw_state)
accel = STAccel()
hid = pyb.USB_HID()

while True:
    if sw_state:
        x, y, z = accel.xyz()
        hid.send((0, int(x * MAG), int(-y * MAG), 0))

    pyb.delay(int(1000 / FREQ))
Note that STAccel.xyz() returns a tuple of floats between 0..1, so the values must be magnified and converted to ints to use them for the relative mouse movement coordinates.

Press and release USER button to turn on sending mouse events, press again to turn it off.

gk26
Posts: 2
Joined: Thu Mar 12, 2015 10:14 pm

Re: STAccel script in STM32F4 DISC

Post by gk26 » Wed Apr 08, 2015 10:39 pm

Thank you guys.
I found some trash-100 files in PYBFLASH. Also I noticed, my board was trying to run led blinking script I had previously wrote and deleted. Not sure of correct process, I just reflashed the board with new firmware, tried STAccel script again with the suggested fix and it worked.

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: STAccel script in STM32F4 DISC

Post by dhylands » Thu Apr 09, 2015 3:50 pm

gk26 wrote:Thank you guys.
I found some trash-100 files in PYBFLASH. Also I noticed, my board was trying to run led blinking script I had previously wrote and deleted. Not sure of correct process, I just reflashed the board with new firmware, tried STAccel script again with the suggested fix and it worked.
The trash-100 files were probably left behind by the Host OS when it connected to the flash drive.

Reflashing the pyboard with new firmware generally doesn't touch the contents of the flash drive.

Hitting RESET or power cycling at just the wrong time can leave the flash drive in an invalid state which will cause it to be reinitialized when the next time the firmware starts up. This is because rewriting things requires a read-erase-write cycle. If the board gets reset after the erase starts and before the write completes, then the internal drive can get left in an invalid state (it depends on exactly which block this happens to).

If you delete a file using the host, the host will typically buffer that delete. If you reset the pyboard before that buffered data is actually written out, then the deleted file may reappear.

User avatar
timotet
Posts: 6
Joined: Sun Dec 14, 2014 12:01 am
Location: Bend, Or.
Contact:

Re: STAccel script in STM32F4 DISC

Post by timotet » Thu Apr 09, 2015 4:43 pm

I just tested this myself, git the same error and tracked it down.

It seems that the call to STAccel.read_id() in STAccel.__init__() always returns 255 instead of the correct value 63. In fact, the first read on the SPI bus after creating the pyb.SPI instance seems to always return 255.

So I just fixed this by reading the ID again:

Code: Select all
class STAccel:
def __init__(self, cs='PE3', spi=1, debug=False):
[...]
self.read_id()
# First SPI read always returns 255 --> discard and read ID again
self.who_am_i = self.read_id()


and then everything works as it should.
Thanks, I was trying to figure that out myself to no avail.

manitou
Posts: 73
Joined: Wed Feb 25, 2015 12:15 am

Re: STAccel script in STM32F4 DISC

Post by manitou » Sat Apr 11, 2015 11:41 am

hmmm, i just started testing with DISCOVERY, got LEDs to flash, but Accel isn't working, i edited boards/STM32F4DISC/staccel.py... what am i missing?

Micro Python v1.4.1-12-g282ca09-dirty on 2015-04-11; F4DISC with STM32F407
>>> import staccel
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: no module named 'staccel'


also tried

>>> import pyb
>>> accel = pyb.Accel()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'Accel'

Post Reply