Motion ControllerLevel: beginner
You have seen it on many robots, you might have tried it with WII. You tilt the control and the object moves along with your movement. How does it work? In this example, we will show you the basic functionality behind the scene. We will build a small circuit with four LEDs and the Accelerometer module. When you tilt the Accelerometer towards left, right, up or down, the corresponding LED on that direction will flash.
What is an Accelerometer
An accelerometer is an instrument used to measure acceleration force. It is also a sensor that measures gravitational force.
On the Education Board, place four LEDs in a form of a cross to indicate left, right, front and back. Connect the positive leg of the LED to PORTs P20, P21, P22, P23 and the negative leg to common ground. You can also add more LEDs to indicate upside down or face down. Then connect the respectively resistors and jumper wires for each LED.
Connect the peripheral Accelerometer. module with the cmdBusTM cable to the cmdBus pins on the Eduational Board. Be sure to connect the red cable of the cmdBus to the VDD pin. Otherwise, it would damage the module.
The circuit of our example looks like this:
Is it right or left?
Let's take a look on the sample code now. As for all the peripherals, we must first declare the module at the beginning of the program with keyword Peripheral and its unique identifier at the ID set on the module. For more information on how to set module to the right ID, see the ID MAP. For a complete list of identifiers for all the peripherals, click here.
Peripheral myG As Accelerometer3A @ 0 ' set module to ID 0
After assigning the pins for each led, we must configure the Accelerometer module so it knows what to do. There are 4 different level of sensing sensitivity. Choose the appropiate level according your needs. Level 0 has the lowest accerelation value which is suitable for our purpose. We also need to indicate how often it should refresh to get the latest values. There 5 different frequency rate. The default is 1 at 20ms, 50Hz. Then we need to define how to determine which plane or axis with SetAxis2D.
Although the module comes with its default setting, you can change the configuration based on your application's needs. The Accelerometer has an extensive list of functions supporting many features.. It can be used to get the force or angle on x, y and z axes. Set force limit and trigger alerts when certain events ocurr.
'set the sensing sensitivity. When the measured Force = 800, 'it is normalized to 1g, suitable for acceleration value within ±1.5g. myG.SetMode(0) 'Set refresh frequency to 100ms (10Hz), default = 1 (20ms (50Hz)) myG.SetRefreshFreq(3) ' X axis = 0-degree axis and Y axis = 90-degree axis myG.SetAxis2D(0)
Before you can get the value of the x, y, z axis, we must get the refresh status first. Once you get the status, we can measure the acceleration value of X, Y and Z- axis using the corresponding function for each axis.
Do g_bStatus = myG.GetRefreshStatus() Loop Until g_bStatus=1 myG.GetXADVal(wADx) ' for x-axis myG.GetYADVal(wADy) ' for y-axis myG.GetZADVal(wADz) ' for z-axis
The returned value of this function is stored in wADX ranging from 0~65535. We use this value to determine which direction the accelerometer was turned to. Based on this value, the corresponding LED on that direction will flash. For more information on acceleration value on each axis, see page 4 on the Accelerometer Manual.
For example, if you hold the accelerometer flat, the returned Z-axis voltage value is less than 1100, all the LEDs will blink. If you hold it upside down flat, the voltage value on Z-axis is greater than 2900, all the LEDs wiil be turned off. Similarly if you tilt the accelerometer to left or right on the X-axis, the right or left LED flashes. If you tilt it upward or downward on the Y-axis, the up or down LED flashes.
If wADy > 2800 Then FlashLed(lpin) ' left ...
To make the LED flash, we use Flashled subroutine. The argument is the number of corresponding pin to which the LED is connected. We use the High/Low command to turn the LED on/off in a Do-Loop to keep it flashing until the next movement. We check the accelerometer has changed its direction when we get the return status from GetRefreshStatus() is 1.
Although this is a simple application, the LEDs and the corresponding resistors must be conencted correctly. Double check if the LED positve and negatives are terminated to ports and ground respectively. Compile and download the code to the Basic Commander. Make sure you are using battery or power adapter for the Accelerometer to work.
Hold the accelerometer at the flat position first and then tilt to a different position. There might be a delay for LEDs to turn on/off if you set Pause too long. If you don't see the LEDS flash, check for the code and the circuit again. As always, in case of error, test your code with Debug command to see if the x, y and z axis voltage values are measurement correctly.
What is Next?
Accelerometer module can used in many application. You can apply the same concept to move theInnobot by tilting the module to the direction you want. For a more challenging project, you can write a program to turn your robot to the right position if it got turned over. Share your ideas or projects with us, or post your suggestions on Forum.
FAQ | Terms & Conditions | Contact Us