Recommended Posts

A small project has been to try to use the Spike Prime acceleration data and Newton’s Laws of Motion to calculate distance moved by a vehicle.

The upshot of working through this has been:

. that the Lego documentation is woefully lacking. ..it is not at all clear what the units are although given the the z acceleration is of the order of 989 I imagine that to be gravity in cm/s/s.

. even though the hub is at rest it reports small accelerations on x and y.

. applying the code ... integrating acceleration over time... the results are inconsistent even at standstill.... more so under movement. It is possible that there is a coding error but unlikely (famous last words!)

So.... any ideas gratefully received!

 

Thanks

Share this post


Link to post
Share on other sites

It could also be milliGs where 1G = 9.81 m/s/s. This is a common unit of measurement used on accelerometer chips.

I think this experiment would be difficult since there is usually a short period of rapid acceleration followed by long period of constant velocity. The period of acceleration would need to be significantly longer than the sample rate of the accelerometer and the acceleration would need to be very smooth.

Share this post


Link to post
Share on other sites

As small addition:

If the hub is not perfectly leveled it will measure acceleration on x/y axes.

If you only measure the acceleration, changes in speed or angle will be indistinguishable, if I remember correctly the hub has a gyro sensor for angle/change in angle measurement. Maybe it's useful for corrections.

Depending on the calculation method it could be useful to flatten/smooth the measurements before integrating.

And if it's more of a theoretical experiment rather than for practical use: Maybe use a longer slide with adjustable angle as track and gravity+counterweight for acceleration.

Share this post


Link to post
Share on other sites

From my experience with a similar project but with an Android device:

- As @David Lechner said; long, smooth accelerations work best.

- Even with long, smooth acceleration, data inaccuracy will build up quickly. You will likely want to record the data at a high sample rate but whatever rate you choose, that will be the rate at which inaccuracies accumulate. These inaccuracies might come from rounding or from the sensor itself. 

- It is impossible for the device/hub to know when it has come to a standstill without some outside input (a constant velocity is the same as a a standstill in terms of acceleration). Because of the previous point, when returning to a standstill from movement it is highly likely that your calculations will show that the device is still moving and hence your distance measurement will drift. This drift will also occur during movement. 

- Treating velocities of say -0.1 to 0.1m/s as 0 did help prevent drift at standstills for me but introduced other obvious problems. 

Good luck!

Edited by ord

Share this post


Link to post
Share on other sites

After making various minor refinements to the code I have at last reproduced consistent results when the robot is at rest however it seems impossible to take this any further as the robot does not return angular measurements to a sufficient degree of accuracy.... eg roll angles are reported only as integers. This means that it is impossible to accurately remove the influence of gravity.

Share this post


Link to post
Share on other sites
10 hours ago, Skookumjim said:

... the robot does not return angular measurements to a sufficient degree of accuracy.... eg roll angles are reported only as integers.

Perhaps you could deduce more accurate angles from the accelerations in x/y/z.

Share this post


Link to post
Share on other sites

Thats how I got consistent results when static... unfortunately  it only works when there is no acceleration (other than gravity).

Share this post


Link to post
Share on other sites

Sorry to ask a dumb newbie type question, but can you tell me how to access the acceleration data in python or in the word blocks?  With Mindstorms Robot Inventor, I can see the instantaneous acceleration values in the ap, but I've only been able to access pitch, roll, and yaw, and not the accelerations, via code.

Share this post


Link to post
Share on other sites

Not at all a dumb question!

Lego segmented the market into Educational and Home... Spike Prime and Robot Inventor.

Each has its own firmware ..and software.....both firmwares run on exactly the same physical hub.

Each offers facilities that the other doesn't. For example Spike Prime enables graph plotting while Robot Inventor enables remote control.

Robot Inventor does not give access to acceleration data, Spike Prime does.

I bought 2 Robot Inventor sets and changed the firmware in one hub to Spike Prime.... hence being able to access acceleration data.

(buying a discounted second RI was far more cost effective than buying a Spike Prime hub!)

 

Share this post


Link to post
Share on other sites

They are actually using the same "firmware" - it's just that both aren't always updated simultanously.

Yeah, the scratch programming blocks differ but both can use the same python commands.

Share this post


Link to post
Share on other sites

the code example below first imports the "hub" module then acceleration data is retrieved by using the hub_status.get command.

import hub

hub_status = hub.status()
acc = hub_status.get('accelerometer')
acc_x = acc[0]
acc_y = acc[1]
acc_z = acc[2]

Cheers Carsten

Edited by Munchkin255
Fixed typo

Share this post


Link to post
Share on other sites

Thanks, @Skookumjim , @Tcm0 , and @Munchkin255 !  Very cool!  And great that it's actually very easy (though not intuitive) to switch between Mindstorms Robot Inventor features and Spike Prime features, just import hub instead of import MSHub!  

 

Share this post


Link to post
Share on other sites

In my previously post I hadn't included all the "standard" includes that Spike Prime adds to any new program.

The "normal" hub features are accessed via the spike module.

Importing the hub modul is done only to access the raw acceleration data.

Here is the complete program for viewing acceleration data.

from spike import PrimeHub, LightMatrix, Button, StatusLight, ForceSensor, MotionSensor, Speaker, ColorSensor, App, DistanceSensor, Motor, MotorPair
from spike.control import wait_for_seconds, wait_until, Timer
from math import *
import sys

import hub
primt_hub = PrimeHub() 

hub_status = hub.status()
acc = hub_status.get('accelerometer')
acc_x = acc[0]
acc_y = acc[1]
acc_z = acc[2]

print (acc)
sys.exit()

cheers Carsten

Share this post


Link to post
Share on other sites
9 hours ago, Munchkin255 said:

In my previously post I hadn't included all the "standard" includes that Spike Prime adds to any new program.

The "normal" hub features are accessed via the spike module.

Importing the hub modul is done only to access the raw acceleration data.

Here is the complete program for viewing acceleration data.


from spike import PrimeHub, LightMatrix, Button, StatusLight, ForceSensor, MotionSensor, Speaker, ColorSensor, App, DistanceSensor, Motor, MotorPair
from spike.control import wait_for_seconds, wait_until, Timer
from math import *
import sys

import hub
primt_hub = PrimeHub() 

hub_status = hub.status()
acc = hub_status.get('accelerometer')
acc_x = acc[0]
acc_y = acc[1]
acc_z = acc[2]

print (acc)
sys.exit()

cheers Carsten

Thanks Carsten.  Interestingly, my experience was a little different.  I was able to access both acceleration and position using "import hub" rather than "import MShub" within the Mindstorms (robot inventor) app and using the other imports that are default for Mindstorms robot inventor.  BUT, the syntax was different than what you wrote.  In my environment, hub.status has no function "get", and instead I had to use

p1, p2, p3 = hub.motion.position()

#the above are pitch, roll and yaw but I haven't figured out which is which yet 

ax, ay, az = hub.motion_accelerometer_filter()

#or leave off _filter and get noisier output

I guess these differences are because I am using the Mindstorms app, rather than downloading whatever you use for Spike Prime?  Maybe?

Share this post


Link to post
Share on other sites

Hi,

I just got my 51515. (I hate that there is no 3 char name for this set :sing:). \

 

On 1/30/2021 at 11:33 PM, sunspot said:

from spike import PrimeHub, LightMatrix, Button, StatusLight, ForceSensor, MotionSensor, Speaker, ColorSensor, App, DistanceSensor, Motor, MotorPair
from spike.control import wait_for_seconds, wait_until, Timer
from math import *
import sys

import hub
primt_hub = PrimeHub() 

hub_status = hub.status()
acc = hub_status.get('accelerometer')
acc_x = acc[0]
acc_y = acc[1]
acc_z = acc[2]

print (acc)
sys.exit()

I assume that the code above to access accelerometer is in python. Is there a way to access accelerometer data via Scratch?

Jordi

Share this post


Link to post
Share on other sites

Sadly not..... and you are not alone in wanting to use Scratch.( I am avoiding python at the mo... not sure I have the patience to learn yet another programming language.. especially one where I dont have access to decent documentation )

Share this post


Link to post
Share on other sites

@Skookumjim Working on a similar project to get coordinations when starting position is known. 

I would love to see your code, whether I can do something new with it / build on it and on my own code. Just some of my ideas which i will take a look at are: 

  • / a new SPIKE Prime app 3.0 has been released (with changes to how code runs - very likely faster and maybe more precise data reading...)
  • / tuning the algorythm for the exact gyro and accelerometer - each one brick is writing slightly different data 

Could you please send it to my email: pomocpeto@gmail.com ? Also thank you very much in advance, I am sure it is worth of trying to get it running. 

Edited by MamOtazku

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.