View Full Version : Negative value for accelerometer

05-31-2010, 11:59 AM
*Sorry if i post at the wrong section, i can't delete this post so I reused it to not double post.*

Hi i'm working with the MXD2125 acc and I trying to program it to drive the motor's PWM.
Anyway, take a look at my codes. I found this code as a base and editted it to suit my goal.

CON ''General Constants for Propeller Setup
_XINFREQ = 5_000_000

xin = 17
yin = 16
period = 16665 ' number of microseconds for 60Hz frequency of pulses
apin = 13 ' output pin for motor a - left motor
bpin = 14 ' output pin for motor b - right motor

memsic_mid = 400_000 ' middle of memsic range
memsic_range = 300 ' range reading divided by 100 to produce percentage
angle_set = 0
maxmemsic = 255 ' maximum possible reading from memsic

OBJ ''Setup Object references that make this demo work
pst : "Parallax Serial Terminal"
MM2125 : "memsic2125"
PWM : "PWM_32_v2"

PUB Main_Program | dir_a,dir_b,i,forward, forward1,forward2, backward, backward1, backward2

pst.Start(115200) '' Initialize serial communication to the PC

MM2125.Start(17, 16) '' Initialize Mx2125

PWM.Start '' Initialize PWM cog

''set up robot direction and speed using memsic X value

'' get x and y readings from memsic

forward := MM2125.Mx ''get memsic x reading
backward := MM2125.Mx

'' subtract mid range value from memsic readings
forward1:= forward - memsic_mid ''subtract mid range from reading to get a value from -100,000 to +100,000
backward1:= backward - memsic_mid

'' Convert memsic readings to a number between 0 > 100
forward2 := forward1 / memsic_range ''now have a value from -100 to +100
backward2 := backward1 / memsic_range

pst.Str(String (pst#CE, pst#HM, "Forward Axis= "))
pst.Str(String (pst#CE, pst#NL, "Backward Axis= "))

As you can see, I am now focusing on the X axis of the acc. I'll put Y axis at a later stage.
What i am trying to achieve is to set the backward axis to a negative value.

At level, both forward and backward axis shows zero. When pitch up, forward axis increase, say 25 degrees. So backward axis will show and set to a negative value ( -25 degrees.)

The reason for wanting that is because I can set the if..else command to control the motor.

If forward > 0, forward motor speed slows down. Backward motor speed increase. until it balances. vice versa

Any kind of codes would be greatly appreciated.

P.S Can somebody try the code above to see if the scaling is right? Mines reading 28+- degrees when placed on the table.

Post Edited (SteelHeart) : 5/31/2010 4:55:08 PM GMT

Mike Green
05-31-2010, 12:06 PM
When you defined MM2125, you used the name of the demo program rather than the name of the I/O driver object. There is no "start" method in the demo program.

05-31-2010, 07:32 PM

Goto the Sticky on the top of the Propeller Forums named "Propeller Education Kit Labs, Tools, and Applications"

There is a section on using Full Duplex with the Parallax Serial Terminal.


06-01-2010, 06:12 AM
I think you just want to use "backward := -MM2125.Mx" instead of "backward := MM2125.Mx." (Note the minus sign in front of the Mx value.)

Then you'll have to set backward1 to "backward1:= backward + memsic_mid"