Question re Hitachi H48C Tri-Axis Accelerometer Module
Brian5084
Posts: 9
Hello all,
I have just purchased the Hitachi H48C Tri-Axis Accelerometer Module and I am having a bit of trouble programming it.· What I want it to do is change pitch (using the speaker) when tilted x and y and operate the servo according to x-axis input.
Any help would be appreciated.
Thanks
Brian
I have just purchased the Hitachi H48C Tri-Axis Accelerometer Module and I am having a bit of trouble programming it.· What I want it to do is change pitch (using the speaker) when tilted x and y and operate the servo according to x-axis input.
Any help would be appreciated.
Thanks
Brian
Comments
IF loop = x axis
xForce = gForce
ELSEIF loop = y axis
yForce = gForce
That goes near the end of main, just before the debug. Then, replace the PAUSE 200 just below that with something like this:
Calculate PULSOUT values
LOOP 10 times
--PULSOUT servos
--Pause 20
To get a general overview of programming, take a look at "What's a Microcontroller" (in the downloads section of the Parallax website).
Just be sure you don't use the word "loop" since LOOP is a reserved word in PBASIC 2.5. I don't believe that the IDE is case sensitive, thus it sees any form of "loop" the same as LOOP (the PBASIC command).
I realize what was offered was just pseudo-code, but I didn't want you to be pulling your hair out over nothing.
Regards,
Bruce Bates
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
When all else fails, try inserting a new battery.
Any suggestions?
Dio PIN 15 ' data to/from module
Clk PIN 14 ' clock output
CS PIN 13 ' active-low chip select
'
[noparse][[/noparse] Constants ]
XAxis CON 0 ' adc channels
YAxis CON 1
ZAxis CON 2
VRef CON 3
Cnt2Mv CON $CE4C ' counts to millivolts
' 0.80586 with **
GfCnv CON $3852 ' g-force conversion
' 0.22 with **
'
[noparse][[/noparse] Variables ]
axis VAR Nib ' axis selection
rvCount VAR Word ' ref voltage adc counts
axCount VAR Word ' axis voltage adc counts
mVolts VAR Word ' millivolts
gForce VAR Word ' axis g-force
XForce VAR Word
YForce VAR Word
dValue VAR Word ' display value
dPad VAR Nib ' display pad
'
[noparse][[/noparse] EEPROM Data ]
'
[noparse][[/noparse] Initialization ]
Reset:
HIGH CS ' deselect module
'
[noparse][[/noparse] Program Code ]
Main:
FOR axis = XAxis TO ZAxis ' loop through each axis
GOSUB Get_H48C ' read vRef & axis counts
' calculate g-force
' -- "gForce" is signed word
IF (axCount >= rvCount) THEN
gForce = (axCount - rvCount) ** GfCnv ' positive g-force
ELSE
gForce = -((rvCount - axCount) ** GfCnv) ' negative g-force
ENDIF
'NEW CODE
IF axis = XAxis THEN 'detect output and store gForce into XForce
XForce = gForce
ELSEIF axis = YAxis THEN 'detect output and store gForce into YForce
YForce = gForce
ENDIF
FREQOUT 1, 50, (100+(YForce * 100)) 'pitch changes according to Y gForce
'END NEW CODE
NEXT
PAUSE 1
GOTO Main
'
[noparse][[/noparse] Subroutines ]
' Reads VRef and selected H48C axis through an MCP3204 ADC
' -- pass axis (0 - 2) in "axis"
' -- returns reference voltage counts in "rvCount"
' -- returns axis voltage counts in "axCounts"
Get_H48C:
LOW CS
SHIFTOUT Dio, Clk, MSBFIRST, [noparse][[/noparse]%11\2, VRef\3] ' select vref register
SHIFTIN Dio, Clk, MSBPOST, [noparse][[/noparse]rvCount\13] ' read ref voltage counts
HIGH CS
PAUSE 1
LOW CS
SHIFTOUT Dio, Clk, MSBFIRST, [noparse][[/noparse]%11\2, axis\3] ' select axis
SHIFTIN Dio, Clk, MSBPOST, [noparse][[/noparse]axCount\13] ' read axis voltage counts
HIGH CS
RETURN
Also, if you're posting large amounts of code, please attach your file, rather than pasting it in directly.
Here's what I am trying to do. I have a project for my Engineering and Design class. The assignment was to design a toy. I am making a spaceship that makes engine noises according to Y axis pitch. I have that covered. Now I need the fins to turn with the roll (X axis) using the servo. In other words I want the servo to turn according to the vehicle tipping left or right.
Thanks in advance for any help.
Brian
You can move the fins in several different ways. The key is how you want to convert g force to servo position. Probably the best way would be to have two things:
A filter
An equation
The filter smooths out the data so that your servo's aren't constantly wiggling with each little change in tilt, and the equation is to equate the g force to the servo position. Take a look at this link to find out how to smooth data (it's for the propeller, but the same principles apply). If you want to get fancy or just get some ideas, take a look at the PID loop included in one of the stickies at the top of the Stamps in Class forum (found it here) Finally, once you have the data the way that you want it, you can do something like
pulsewidth = gForce * 100 + servoCenter
Post Edited (SRLM) : 11/27/2008 6:24:01 AM GMT
······· IF axis = XAxis THEN··························· 'detect output and store gForce into XForce
········ XForce = ((gForce/4) * 1000)· + 750··········· 'convert to values between 500 and 1000
······· ELSEIF axis = YAxis THEN······················ 'detect output and store gForce into YForce
········ YForce = gForce
······· ELSEIF axis = ZAxis THEN
········ ZForce = gForce
······· ENDIF
· PULSOUT 14, XForce···································· ' servo control
I also do not understand the smoothing function described above.
Sorry to be so incompetent. ·I am new at programming.
Thanks for your time and patience
One potential problem that I see is that you are not refreshing the servos correctly. A servo needs to have a refresher pulse sent every 20 ms (more or less). So, if your code takes too long or is too fast, that can create problems. If it's too fast, then you can just add a pause statement, and if it's too slow, then you can cut all the x axis stuff (since you're not using it).
Another problem is that your xForce variable may be more or less than the 1000-2000 range that is ideal. You should have a something that tests and corrects for this. I'm not sure what the gForce capabilities of the accelerometer are, but you are probably using a 'delicate' touch when using it as you are. However, if you were to drop the module into free fall, then gForce is 0 (potentially), and you pulsout 750.
And no need to worry about asking questions: we all have to learn sometime, and questions are the best indication that you're trying to grasp a topic.
Brian