Map servo function causing failure in UAV

First time user so apologies for being uninformed on posting procedures ECT.

My current project is stabilizing a flying wing UAV using a MPU which I use to correct the rudders on each wing. This works fine if I write an actual value for the servo write function: RightServo.write(0)
LeftServo.write(110).

However, if replace the value and instead try this function:
RightServo.write(map(mpu.getAccX(), 0.20, 0.45, 60 ,0))

Serial monitor doesn't show any data and the UAV is unresponsive

So, what am I doing? I'm trying to get the "servo/Rudder" to move proportionally based on the amount of angle I rotate the UAV as I hold it in bench testing.

I have tried defining the value to be mapped as a: int, long, float, etc.
and it doesn't change the result


I don't see how I can share my code, besides copy and paste, which won't look appealing? Suggestions
Thank you.

For posting advice, please read and follow the instructions in "How to get the best out of the forum", linked at the head of every topic.

For information on Arduino functions like map(), consult the Arduino Reference pages. map() - Arduino Reference

Thank you, had to goggle "How to get the best out of the forum" as I failed to see it after scrolling in site.

map() does integer math only.


#include <Wire.h>
#include "i2c.h"
# include <Servo.h>
Servo LeftServo;
Servo RightServo;
void setup() {
  LeftServo.attach(6);
RightServo.attach(5);
//-------------------------------------------------
RightServo.write(50);
LeftServo.write(60);

 Wire.begin();
  
  byte status = mpu.begin();
 Serial.print(F("MPU6050 status: "));
 Serial.println(status);
  while(status!=0){ } // stop everything if could not connect to MPU6050
  
 Serial.println(F("Calculating offsets, do not move MPU6050"));
  delay(1000);
  mpu.calcOffsets(true,true); // gyro and accelero
 Serial.println("Done!\n");
}

void loop() {
  // MPU 6050
 mpu.update();

  if(millis() - timer > 500){ // print data every .5 second
   // Serial.print(F("TEMPERATURE: "));Serial.println(mpu.getTemp());
  Serial.print(F("ACCELERO  X: "));
  Serial.println(mpu.getAccX());
   Serial.print("\tY: ");
   Serial.println(mpu.getAccY());
  Serial.print("\tZ: ");
  Serial.println(mpu.getAccZ());}


  if (mpu.getAccY()>.30){
  RightServo.write(0);
LeftServo.write(0);
delay(10);
  }
  else if (mpu.getAccY()<-.30){
  RightServo.write(110);
LeftServo.write(110);
delay(10);
  }
  else  {RightServo.write(50);
LeftServo.write(60);}

}

Thank you for response, the values I get from the IMU are from decimals from 0-1
so, if map only accepts integers than can I simply multiply them by a magnitude of 100 to resolve the issue.

Or write your own function to handle the scaling. map() is extremely simple.

I agree had much success using it in past engineering prototypes
first time hitting a wall with it.
Thank you.

@jremington
Thank you very much for the patience and guidance you solve my problem

SOLUTION : as mentioned earlier I multiplied the decimal values by order of 100
and the
*NEW_VALUE = mpu.getAccX()100 I had to make it a FLOAT type since ( int , long ) did not suffice

Now the rudders maintain parallel to ground while UAV body rotates.

Very grateful have been using these forums for years and most issues have been resolved so i never needed an account till now.

What values are you getting from this?

see post #6

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.