Advertisement
Guest User

Untitled

a guest
Feb 16th, 2013
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* Monitor a 3 axis accelerometer and control servos to keep sensor level
  2.    If arm is upright then keep sensor level above arm, if inverted keep level below arm
  3.    Servos should not move out ouf range
  4.    All values and bargraphs from 0G to 1G displayed on 4 line by 20 char LCD
  5.    atmega328p is being used on nano arduino
  6.    Version 0.1 RifRaf 17-2-2013 - first version
  7.    Version 0.2 - revised the code to remove 150 odd lines */
  8.  
  9. #include "hardware.h"
  10.  
  11. // Initialise the hardware
  12. void appInitHardware(void) {
  13.     initHardware();
  14. }
  15. // Initialise the software
  16. TICK_COUNT appInitSoftware(TICK_COUNT loopStart){
  17.     rprintfInit(displayGetWriter(&display));
  18.     rprintf("ADXL345Accelerometer");  //Setup LCD for chars that will not change
  19.     return 0;
  20. }
  21. // This is the main loop
  22. TICK_COUNT appControl(LOOP_COUNT loopCount, TICK_COUNT loopStart) {
  23.  
  24.     rprintfInit(displayGetWriter(&display));
  25.     //Check accelerometer and create value for X Y Z
  26.     accelerometerRead(acceleration);
  27.     int valX = acceleration.accelerometer.x_axis_mG;
  28.     int valY = acceleration.accelerometer.y_axis_mG;
  29.     int valZ = acceleration.accelerometer.z_axis_mG;
  30.  
  31.     if (valZ >=0) {  //Check to see if arm is upright
  32.         DRIVE_SPEED speed=act_getSpeed(&servo2);
  33.         int move =  valX/125; // Greater valX will make arm take a larger step towards level
  34.         speed = speed + move;
  35.         act_setSpeed(&servo2,speed);  //Move arm
  36.     }
  37.  
  38.     if (valZ <=-1) {  //Check to see if arm is upside down
  39.         DRIVE_SPEED speed=act_getSpeed(&servo2);
  40.         int move =  -valX/125; // Greater valX will make arm take a larger step towards level
  41.         speed = speed + move;
  42.         act_setSpeed(&servo2,speed);  //Move arm
  43.     }
  44.  
  45.     displayGoto(&display,0,1);  // Print valX to LCD
  46.     rprintf("X %d mG  ",valX);
  47.         if (valX < 0) {
  48.          valX = -valX;
  49.     }
  50.     displayHorizGraph(&display,10,1,valX,1000,10);
  51.  
  52.     DRIVE_SPEED speed=act_getSpeed(&servo3);
  53.     int move =  valY/125; // Greater valX will make arm take a larger step towards level
  54.     speed += move * ((valZ > 0) - (valZ < 0));
  55.     act_setSpeed(&servo3,speed);  //Move arm
  56.  
  57.  
  58.     displayGoto(&display,0,2);  //Display valY to LCD
  59.     rprintf("Y %d mG  ",valY);
  60.         if (valY < 0) {
  61.          valY = -valY;
  62.     }
  63.     displayHorizGraph(&display,10,2,valY,1000,10);
  64.  
  65.     displayGoto(&display,0,3);  //Display valZ to LCD
  66.     rprintf("Z %d mG  ",valZ);
  67.         if (valZ < 0) {
  68.          valZ = -valZ;
  69.     }
  70.     displayHorizGraph(&display,10,3,valZ,1000,10);
  71.  
  72.     return 0;
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement