Vernier Acceleration Sensor Python: Difference between revisions

From wikiluntti
 
(10 intermediate revisions by the same user not shown)
Line 1: Line 1:
=== Introduction ===
=== Introduction ===


=== Python: Check the modes ===
=== Sensor Class: Check the modes ===
 
Actually, the following codes assume that the color sensor is attached to the port 1. It should be "nxt-analog".


<syntaxhighlight lang="python">
<syntaxhighlight lang="python">
Line 11: Line 13:


import time
import time
import os
os.system('setfont Lat15-TerminusBold32x16')  # Try this larger font


p = Sensor( INPUT_1 )
p = Sensor( INPUT_1 )
Line 17: Line 21:
     time.sleep(5)
     time.sleep(5)
</syntaxhighlight>
</syntaxhighlight>
Returns the following modes 'ref-raw', 'rgb-raw' and 'col-cal'
Returns the following modes 'analog-0' and 'analog-1'.


==== REF-RAW ====
Or equivalently the following code works


Not working.
<syntaxhighlight lang="python">
from ev3dev2.sensor import *


==== RGB-RAW ====
import time


Not working.
accSensor = Sensor(address="ev3-ports:in1")
print( accSensor )
print( accSensor.modes )
#time.sleep(5)
accSensor.mode="ANALOG-0"  #
#accSensor.mode="ANALOG-1"  #
#time.sleep(5)
while 1:
    print(accSensor.value() )
</syntaxhighlight>
The mode ''ANALOG-0'' and ''ANALOG-1'' differ by that that pin 5 is high for ''ANALOG-1'', see
[http://docs.ev3dev.org/projects/lego-linux-drivers/en/ev3dev-jessie/sensor_data.html#nxt-analog-modes | nxt-analog modes].


==== COL-CAL ====
=== Calibrating The Accelerometer ===


Not working.
The Vernier accelerometer measures one dimensional acceleration. Thus, it is easy to calibrate by using Earth's gravitational acceleration, <math>g = 9.81 m/s^2</math>.
 
<syntaxhighlight lang="python">
#!/usr/bin/env python3
# https://sites.google.com/site/ev3devpython/
 
from ev3dev2.sensor import *
from ev3dev2.sensor import INPUT_1
 
import time
import os
os.system('setfont Lat15-TerminusBold32x16')  # Try this larger font
 
p = Sensor( INPUT_1 )
p.mode="ANALOG-1"
 
while 1:
    acc = ( p.value(0)-2271)/73*9.81
    print( acc )
    time.sleep(1)
 
    #Down 2198
    #-> Zero : 2198 + 146/2 = 2271
    #Up 2344
</syntaxhighlight>

Latest revision as of 20:20, 21 September 2020

Introduction

Sensor Class: Check the modes

Actually, the following codes assume that the color sensor is attached to the port 1. It should be "nxt-analog".

#!/usr/bin/env python3
# https://sites.google.com/site/ev3devpython/

from ev3dev2.sensor import *
from ev3dev2.sensor import INPUT_1

import time
import os
os.system('setfont Lat15-TerminusBold32x16')  # Try this larger font

p = Sensor( INPUT_1 )
while 1:
    print( p.modes )
    time.sleep(5)

Returns the following modes 'analog-0' and 'analog-1'.

Or equivalently the following code works

from ev3dev2.sensor import *

import time

accSensor = Sensor(address="ev3-ports:in1")
print( accSensor )
print( accSensor.modes )
#time.sleep(5)
accSensor.mode="ANALOG-0"   #
#accSensor.mode="ANALOG-1"  #
#time.sleep(5)
while 1:
    print(accSensor.value() )

The mode ANALOG-0 and ANALOG-1 differ by that that pin 5 is high for ANALOG-1, see | nxt-analog modes.

Calibrating The Accelerometer

The Vernier accelerometer measures one dimensional acceleration. Thus, it is easy to calibrate by using Earth's gravitational acceleration, .

#!/usr/bin/env python3
# https://sites.google.com/site/ev3devpython/

from ev3dev2.sensor import *
from ev3dev2.sensor import INPUT_1

import time
import os
os.system('setfont Lat15-TerminusBold32x16')  # Try this larger font

p = Sensor( INPUT_1 )
p.mode="ANALOG-1"

while 1:
    acc = ( p.value(0)-2271)/73*9.81
    print( acc )
    time.sleep(1)

    #Down 2198
    #-> Zero : 2198 + 146/2 = 2271
    #Up 2344