Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Need the following code to be edited for another accelerometer I am using adxl345 instea dof MMA8452Q //I used 4.5 flex sensor and MMA8452Q acceleration

Need the following code to be edited for another accelerometer I am using adxl345 instea dof MMA8452Q

//I used 4.5" flex sensor and MMA8452Q acceleration sensor found in sparkfun's essential sensor kit. //altitude - fly higher or lower //yaw - turning around in places around Y axis of the drone, didnt use it in the end :/ // directionXY - controls the direction of the drone

// inslude the SPI library: #include #include // Must include Wire library for I2C #include // Includes the SFE_MMA8452Q library #include

MMA8452Q accel;

// set pin 10 as the slave select for the digital pot, otherwise it wont work, probably a bug

int XaccelLow; int XaccelHigh; int YaccelLow; int YaccelHigh; int b = 1; int flexSensorReading = 0; int flex0to255 =0; int lowest = 0; int highest = 0; double lowest1; double highest1; double lowest2; double highest2; double ax; double ay; double az; double xAngle; double yAngle; double zAngle; int accelx; int accely;

const int buzzer = 6; //buzzer connected to pin 6 const int flexSensorPin = A0; //conncted to pin A0 const int DefaultslaveSelectPin = 10; const int slaveSelectPin0 = 8; const int slaveSelectPin1 = 9; const int shutdownPin = 7; const int wiper0writeAddr = B00000000; //these addresses can be found in your chip's datasheet const int wiper1writeAddr = B00010000; SPISettings manochipui(10000000, MSBFIRST, SPI_MODE0); //these settings might be different for your chip, mine is MCP4251, found some of these settings in datasheet, for others just tested digipot+LED with different settings.

void setup() { // delay(5000); pinMode(buzzer, OUTPUT); // set the slaveSelectPin as an output: pinMode (DefaultslaveSelectPin, OUTPUT); pinMode (slaveSelectPin0, OUTPUT); pinMode (slaveSelectPin1, OUTPUT); // set the shutdownPin as an output: pinMode (shutdownPin, OUTPUT); Serial.begin(9600); Serial.setTimeout(50); Serial.println ("ijungt drona, palaukt 1sek kol sumirkses, tada upload spaust, tada 10sek idet laidui is gnd i +5v"); Serial.println ("turn on drone, wait 1 second for the led on drone to flash, then insert USB, then flip power switch to ON on the remote");

digipoton(); //the remote is calibrating itself (same as pushing pitch stick up and down) tone(buzzer, 1000); //this means the remote is done calibrating itself delay(400); noTone(buzzer); flexcalibration(); //calibration of flex sensor accel.init(); delay(2); //accel.init() needs these 2 milisec tone(buzzer, 1000); delay(400); noTone(buzzer); accelcalibrationX(); //calibration of accel sensor's X axis tone(buzzer, 1000); delay(400); noTone(buzzer); accelcalibrationY();//calibration of accel sensor's Y axis tone(buzzer, 500); delay(200); noTone(buzzer); tone(buzzer, 500); delay(200); noTone(buzzer); tone(buzzer, 500); delay(200); noTone(buzzer); }

void loop() {

flexSensorReading = analogRead(flexSensorPin); flex0to255 = map(flexSensorReading, lowest, highest, 0, 255); //takes the lowest and highest points found during flex sensors calibration and maps them from 0 to 255 flex0to255 = constrain(flex0to255, 0, 255); //constrains flex0to255 so it doesnt exceed 255 altitude(flex0to255); // sends data to digipot connected to remote's altitude potentiometer Serial.println(flex0to255);

accel.read(); //this function is from a library. It takes accelerometers output and converts it to Gs in each axis. "accel.cx" means calculated x axis' G. Full explanation is found in sparkfun website about accelerometer delay(2); Serial.print(accel.cx, 3); Serial.print("\t"); Serial.print(accel.cy, 3); Serial.print("\t"); Serial.print(accel.cz, 3); Serial.print("\t"); Serial.println(); ax = accel.cx; ay = accel.cy; az = accel.cz; xAngle = atan( ax / (sqrt(square(ay) + square(az)))); yAngle = atan( ay / (sqrt(square(ax) + square(az)))); zAngle = atan( sqrt(square(ax) + square(ay)) / az); // xAngle *= 180.00; yAngle *= 180.00; zAngle *= 180.00; // xAngle /= 3.141592; yAngle /= 3.141592; zAngle /= 3.141592; //converts Gs to degrees. You cant use Gs for map, because map command cant use real numbers. //They can be converted to thousand of millions integer, by multiplying a real number, but i didnt like it, because i wanted to see the angle in console. xAngle *= 57.29579; yAngle *= 57.29579; zAngle *= 57.29579; Serial.print(xAngle, 3); Serial.print("\t"); Serial.print(yAngle, 3); Serial.print("\t"); Serial.print(zAngle, 3); Serial.print("\t");

Serial.println(); accelx = map(xAngle, lowest1, highest1, 0, 255); //same as flex0to255 but for accelerometer's output accelx = constrain(accelx, 0, 255); accely = map(yAngle, lowest2, highest2, 0, 255); accely = constrain(accely, 0, 255);

Serial.print(accelx); Serial.print("\t"); Serial.print(accely); Serial.print("\t"); Serial.println(); Serial.println(); directionXY(accelx, accely); // sends data to digipot connected to remote's XY potentiometers delay(100); }

void digipoton() { delay(10000);

digitalWrite(shutdownPin,LOW); // start with all the pots shutdown (low = pot is off) digitalWrite(slaveSelectPin0,LOW); //if slaveselect pin is Low then it is active digitalWrite(slaveSelectPin1,LOW); delay(1); SPI.begin(); SPI.beginTransaction(manochipui); altitude(122); //all digipots are set to middle resistance yaw(122); directionXY(122, 122); delay(1); digitalWrite(shutdownPin,HIGH); //all pots are turned on, i changed values before turing them on just in case altitude(122); //change values again, just in case yaw(122); directionXY(122, 122); delay(1); for (int level = 122; level <= 255; level++) { //sets altitude digipot wiper to max altitude(level); delay(10); } delay(500); for (int level = 255; level >= 0; level--) { //sets altitude digipot wiper to min altitude(level); delay(10); } delay(500); }

void flexcalibration() { //gal padaryt kad laikai atpalaidaves ranka, tai 0, tada pradedi spausti iki max, skaito vidutines reiksmes kas 0.1sek, //skaitymas prasideda kai viena karta flexini kazkiek, tada 5 sekundes skaito min max duomenis //gal daryt vidutinio skrydimo diapozonas butu platus, o stipraus ir silpno siauri diapozonai, vistiek tikslaus valdymo reikia tik vidutiniame stiprume. Serial.println("flex calibration for at least 10 sec"); lowest = analogRead(flexSensorPin); highest = analogRead(flexSensorPin);

for (int i=0; i<10000;i++) //finds min and max values of flex sensor, so you can use any resistance you want, doesnt have to be 22kohms { if ( analogRead(flexSensorPin)>lowest ) { lowest = analogRead(flexSensorPin); } if ( analogRead(flexSensorPin)

} Serial.print("flex min = "); Serial.println(lowest); Serial.print("flex max = "); Serial.println(highest); delay(2000); Serial.println("flex kalibravimas baigtas"); }

void accelcalibrationX() { Serial.println("accel X calibration for 10 sec"); accel.read(); delay(2); ax = accel.cx; ay = accel.cy; az = accel.cz; xAngle = atan( ax / (sqrt(square(ay) + square(az)))); // yAngle = atan( ay / (sqrt(square(ax) + square(az)))); // zAngle = atan( sqrt(square(ax) + square(ay)) / az); // xAngle *= 180.00; yAngle *= 180.00; zAngle *= 180.00; // xAngle /= 3.141592; yAngle /= 3.141592; zAngle /= 3.141592; xAngle *= 57.29579; // yAngle *= 57.29579; zAngle *= 57.29579; lowest1 = xAngle; highest1 = xAngle + 1; delay(2);

for (int i=0; i<5000;i++) { accel.read(); ax = accel.cx; ay = accel.cy; az = accel.cz; delay(2); xAngle = atan( ax / (sqrt(square(ay) + square(az)))); yAngle = atan( ay / (sqrt(square(ax) + square(az)))); zAngle = atan( sqrt(square(ax) + square(ay)) / az); // xAngle *= 180.00; yAngle *= 180.00; zAngle *= 180.00; // xAngle /= 3.141592; yAngle /= 3.141592; zAngle /= 3.141592; xAngle *= 57.29579; //yAngle *= 57.29579; zAngle *= 57.29579; if ( xAngle < lowest1 ) { lowest1 = xAngle; } if ( xAngle > highest1 ) { highest1 = xAngle; }

} Serial.print("accel X angle min = "); Serial.println(lowest1); Serial.print("accel X angle max = "); Serial.println(highest1); delay(1000); Serial.println("accel X axis calibration complete"); }

void accelcalibrationY() { Serial.println("accel Y calibration for 10 sec"); delay(2000); accel.read(); delay(2); ax = accel.cx; ay = accel.cy; az = accel.cz; xAngle = atan( ax / (sqrt(square(ay) + square(az)))); yAngle = atan( ay / (sqrt(square(ax) + square(az)))); zAngle = atan( sqrt(square(ax) + square(ay)) / az); // xAngle *= 180.00; yAngle *= 180.00; zAngle *= 180.00; // xAngle /= 3.141592; yAngle /= 3.141592; zAngle /= 3.141592; // xAngle *= 57.29579; yAngle *= 57.29579; // zAngle *= 57.29579; lowest2 = yAngle; highest2 = yAngle + 1;

for (int i=0; i<5000;i++) { accel.read(); delay(2); ax = accel.cx; ay = accel.cy; az = accel.cz; xAngle = atan( ax / (sqrt(square(ay) + square(az)))); yAngle = atan( ay / (sqrt(square(ax) + square(az)))); zAngle = atan( sqrt(square(ax) + square(ay)) / az); // xAngle *= 180.00; yAngle *= 180.00; zAngle *= 180.00; // xAngle /= 3.141592; yAngle /= 3.141592; zAngle /= 3.141592; // xAngle *= 57.29579; yAngle *= 57.29579; // zAngle *= 57.29579;

if ( yAngle < lowest2 ) { lowest2 = yAngle; } if ( yAngle > highest2 ) { highest2 = yAngle; } } Serial.print("accel Y angle min = "); Serial.println(lowest2); Serial.print("accel Y angle min = "); Serial.println(highest2); delay(4000); Serial.println("accel X axis calibration complete"); }

//altitude - fly higher or lower void altitude(int value) { // take the SS pin low to select the chip: digitalWrite(slaveSelectPin1,HIGH); digitalWrite(slaveSelectPin0,LOW); // send in the address and value via SPI: SPI.transfer(wiper0writeAddr); SPI.transfer(value); // take the SS pin high to de-select the chip: digitalWrite(slaveSelectPin0,HIGH); }

//yaw - turning around in places around Y axis of the drone, didnt use it in the end :/ void yaw(int value) { // take the SS pin low to select the chip: digitalWrite(slaveSelectPin1,HIGH); digitalWrite(slaveSelectPin0,LOW); // send in the address and value via SPI: SPI.transfer(wiper1writeAddr); SPI.transfer(value); // take the SS pin high to de-select the chip: digitalWrite(slaveSelectPin0,HIGH); }

// directionXY - controls the direction of the drone void directionXY(int iksas, int ykas) { digitalWrite(slaveSelectPin1,LOW); digitalWrite(slaveSelectPin0,HIGH); SPI.transfer(wiper0writeAddr); SPI.transfer(iksas); SPI.transfer(wiper1writeAddr); SPI.transfer(ykas); digitalWrite(slaveSelectPin1,HIGH); }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

1. What is going on? What do you notice?

Answered: 1 week ago

Question

6. How do histories influence the process of identity formation?

Answered: 1 week ago