Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Monitor a 3 axis accelerometer and control servos to keep sensor level
- If arm is upright then keep sensor level above arm, if inverted keep level below arm
- Servos should not move out ouf range
- All values and bargraphs from 0G to 1G displayed on 4 line by 20 char LCD
- atmega328p is being used on nano arduino
- Version 0.1 RifRaf 17-2-2013 - first version */
- #include "hardware.h"
- // Initialise the hardware
- void appInitHardware(void) {
- initHardware();
- }
- // Initialise the software
- TICK_COUNT appInitSoftware(TICK_COUNT loopStart){
- rprintfInit(displayGetWriter(&display));
- rprintf("ADXL345Accelerometer"); //Setup LCD for chars that will not change
- displayGoto(&display,0,1);
- rprintf("X mG ");
- displayGoto(&display,0,2);
- rprintf("Y mG ");
- displayGoto(&display,0,3);
- rprintf("Z mG ");
- return 0;
- }
- // This is the main loop
- TICK_COUNT appControl(LOOP_COUNT loopCount, TICK_COUNT loopStart) {
- rprintfInit(displayGetWriter(&display));
- //Check accelerometer and create value for X Y Z
- accelerometerRead(acceleration);
- int valX = acceleration.accelerometer.x_axis_mG;
- int valY = acceleration.accelerometer.y_axis_mG;
- int valZ = acceleration.accelerometer.z_axis_mG;
- if (valZ >=0) { //Check to see that arm is upright
- if (valX >=400) { //Long way off, move quick
- DRIVE_SPEED speed=act_getSpeed(&servo2);
- if (speed <= 122){ speed = speed + 5;}
- act_setSpeed(&servo2,speed);
- }
- else if (valX >=200) { //Getting closer, slow down
- DRIVE_SPEED speed=act_getSpeed(&servo2);
- if (speed <= 125){ speed = speed + 2;}
- act_setSpeed(&servo2,speed);
- }
- else if (valX >=100) { //Close so go slower still
- DRIVE_SPEED speed=act_getSpeed(&servo2);
- if (speed <= 126){ speed ++;}
- act_setSpeed(&servo2,speed);
- }
- else if (valX >=50) { //Close enough to just stop
- DRIVE_SPEED speed=act_getSpeed(&servo2);
- act_setSpeed(&servo2,speed);
- }
- if (valX <=-400) { //Next section is just the opposite of abuve
- DRIVE_SPEED speed=act_getSpeed(&servo2);
- if (speed >= -122){ speed = speed - 5;}
- act_setSpeed(&servo2,speed);
- }
- else if (valX <=-200) {
- DRIVE_SPEED speed=act_getSpeed(&servo2);
- if (speed >= -125){ speed = speed - 2;}
- act_setSpeed(&servo2,speed);
- }
- else if (valX <=-100) {
- DRIVE_SPEED speed=act_getSpeed(&servo2);
- if (speed >= -126){ speed --;}
- act_setSpeed(&servo2,speed);
- }
- else if (valX <=-50) {
- DRIVE_SPEED speed=act_getSpeed(&servo2);
- act_setSpeed(&servo2,speed);
- }
- }
- if (valZ <=-1) { // Check to see if upside down and do opposite of above for X axis
- if (valX >=400) {
- DRIVE_SPEED speed=act_getSpeed(&servo2);
- if (speed <= 122){ speed = speed - 5;}
- act_setSpeed(&servo2,speed);
- }
- else if (valX >=200) {
- DRIVE_SPEED speed=act_getSpeed(&servo2);
- if (speed <= 125){ speed = speed - 2;}
- act_setSpeed(&servo2,speed);
- }
- else if (valX >=100) {
- DRIVE_SPEED speed=act_getSpeed(&servo2);
- if (speed <= 126){ speed --;}
- act_setSpeed(&servo2,speed);
- }
- else if (valX >=50) {
- DRIVE_SPEED speed=act_getSpeed(&servo2);
- act_setSpeed(&servo2,speed);
- }
- if (valX <=-400) {
- DRIVE_SPEED speed=act_getSpeed(&servo2);
- if (speed >= -122){ speed = speed + 5;}
- act_setSpeed(&servo2,speed);
- }
- else if (valX <=-200) {
- DRIVE_SPEED speed=act_getSpeed(&servo2);
- if (speed >= -125){ speed = speed + 2;}
- act_setSpeed(&servo2,speed);
- }
- else if (valX <=-100) {
- DRIVE_SPEED speed=act_getSpeed(&servo2);
- if (speed >= -126){ speed ++;}
- act_setSpeed(&servo2,speed);
- }
- else if (valX <=-50) {
- DRIVE_SPEED speed=act_getSpeed(&servo2);
- act_setSpeed(&servo2,speed);
- }
- }
- displayGoto(&display,2,1); // Print valX to LCD
- rprintf("%d",valX);
- if (valX < 0) {
- valX = -valX;
- }
- displayHorizGraph(&display,10,1,valX,1000,10);
- if (valZ >=0) { //See if right way up, control Y axis
- if (valY >=400) {
- DRIVE_SPEED speed=act_getSpeed(&servo3);
- if (speed <= 122){ speed = speed + 5;}
- act_setSpeed(&servo3,speed);
- }
- else if (valY >=200) {
- DRIVE_SPEED speed=act_getSpeed(&servo3);
- if (speed <= 125){ speed = speed + 2;}
- act_setSpeed(&servo3,speed);
- }
- else if (valY >=100) {
- DRIVE_SPEED speed=act_getSpeed(&servo3);
- if (speed <= 126){ speed ++;}
- act_setSpeed(&servo3,speed);
- }
- else if (valY >=50) {
- DRIVE_SPEED speed=act_getSpeed(&servo3);
- act_setSpeed(&servo3,speed);
- }
- if (valY <=-400) {
- DRIVE_SPEED speed=act_getSpeed(&servo3);
- if (speed >= -122){ speed = speed - 5;}
- act_setSpeed(&servo3,speed);
- }
- else if (valY <=-200) {
- DRIVE_SPEED speed=act_getSpeed(&servo3);
- if (speed >= -125){ speed = speed - 2;}
- act_setSpeed(&servo3,speed);
- }
- else if (valY <=-100) {
- DRIVE_SPEED speed=act_getSpeed(&servo3);
- if (speed >= -126){ speed --;}
- act_setSpeed(&servo3,speed);
- }
- else if (valY <=-50) {
- DRIVE_SPEED speed=act_getSpeed(&servo3);
- act_setSpeed(&servo3,speed);
- }
- }
- if (valZ <=-1) { //See if upside down, Control Y axis
- if (valY >=400) {
- DRIVE_SPEED speed=act_getSpeed(&servo3);
- if (speed <= 122){ speed = speed - 5;}
- act_setSpeed(&servo3,speed);
- }
- else if (valY >=200) {
- DRIVE_SPEED speed=act_getSpeed(&servo3);
- if (speed <= 125){ speed = speed - 2;}
- act_setSpeed(&servo3,speed);
- }
- else if (valY >=100) {
- DRIVE_SPEED speed=act_getSpeed(&servo3);
- if (speed <= 126){ speed --;}
- act_setSpeed(&servo3,speed);
- }
- else if (valY >=50) {
- DRIVE_SPEED speed=act_getSpeed(&servo3);
- act_setSpeed(&servo3,speed);
- }
- if (valY <=-400) {
- DRIVE_SPEED speed=act_getSpeed(&servo3);
- if (speed >= -122){ speed = speed + 5;}
- act_setSpeed(&servo3,speed);
- }
- else if (valY <=-200) {
- DRIVE_SPEED speed=act_getSpeed(&servo3);
- if (speed >= -125){ speed = speed + 2;}
- act_setSpeed(&servo3,speed);
- }
- else if (valY <=-100) {
- DRIVE_SPEED speed=act_getSpeed(&servo3);
- if (speed >= -126){ speed ++;}
- act_setSpeed(&servo3,speed);
- }
- else if (valY <=-50) {
- DRIVE_SPEED speed=act_getSpeed(&servo3);
- act_setSpeed(&servo3,speed);
- }
- }
- displayGoto(&display,2,2); //Display valY to LCD
- rprintf("%d",valY);
- if (valY < 0) {
- valY = -valY;
- }
- displayHorizGraph(&display,10,2,valY,1000,10);
- displayGoto(&display,2,3); //Display valZ to LCD
- rprintf("%d",valZ);
- if (valZ < 0) {
- valZ = -valZ;
- }
- displayHorizGraph(&display,10,3,valZ,1000,10);
- delay_ms(20); // Pause so values remain on screen a bit then blank srceen values to get rid of them
- displayGoto(&display,2,1);
- rprintf(" ");
- displayGoto(&display,2,2);
- rprintf(" ");
- displayGoto(&display,2,3);
- rprintf(" ");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement