Pages: [1]   Go Down
Author Topic: BMA180 Control Servo, but servo speed is slow  (Read 657 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Jr. Member
**
Karma: 0
Posts: 72
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi, i have make servo controller using BMA180 accelerometer. This sensor is in this board:
https://www.hobbyking.com/hobbyking/store/__27033__MultiWii_328P_Flight_Controller_w_FTDI_DSM2_Port.html

I successfully control two servo, but the problem is my program make servo move slow, it is not responsive. Is it because I using Map function?
This is my complete code:

Code:
#include <Wire.h>
#include <Servo.h>

Servo pitch;
Servo roll;

void setup()
{
  pitch.attach(A0);
  roll.attach(A1);
 
  Serial.begin(115200);
  Wire.begin();
 
  Serial.println("Demo started, initializing sensors");
 
  AccelerometerInit();
 
  Serial.println("Sensors have been initialized");
}
 
void AccelerometerInit()
{
  Wire.beginTransmission(0x40); // address of the accelerometer
  // reset the accelerometer
  Wire.write(0x10);
  Wire.write(0xB6);
  Wire.endTransmission();
  delay(10);
 
  Wire.beginTransmission(0x40); // address of the accelerometer
  // low pass filter, range settings
  Wire.write(0x0D);
  Wire.write(0x10);
  Wire.endTransmission();
 
  Wire.beginTransmission(0x40); // address of the accelerometer
  Wire.write(0x20); // read from here
  Wire.endTransmission();
  Wire.requestFrom(0x40, 1);
  byte data = Wire.read();
  Wire.beginTransmission(0x40); // address of the accelerometer
  Wire.write(0x20);
  Wire.write(data & 0x0F); // low pass filter to 10 Hz
  Wire.endTransmission();
 
  Wire.beginTransmission(0x40); // address of the accelerometer
  Wire.write(0x35); // read from here
  Wire.endTransmission();
  Wire.requestFrom(0x40, 1);
  data = Wire.read();
  Wire.beginTransmission(0x40); // address of the accelerometer
  Wire.write(0x35);
  Wire.write((data & 0xF1) | 0x04); // range +/- 2g
  Wire.endTransmission();
}
 
 
void loop()
{
 
  Wire.beginTransmission(0x40); // address of the accelerometer
  Wire.write(0x02); // set read pointer to data
  Wire.endTransmission();
  Wire.requestFrom(0x40, 6);
 
  // read in the 3 axis data, each one is 16 bits
  // print the data to terminal
  Serial.print("Accelerometer: X = ");
  short data = Wire.read();
  data += Wire.read() << 8;
  Serial.print(data);
  data=constrain(data,-15000, 15000);
  data = map(data, -15000, 15000, 0, 179);     // scale it to use it with the servo (value between 0 and 179)
  pitch.write(data);
 
  Serial.print(" , Y = ");
  data = Wire.read();
  data += Wire.read() << 8;
  Serial.print(data);
  data=constrain(data,-15000, 15000);
  data = map(data, -15000, 15000, 0, 179);     // scale it to use it with the servo (value between 0 and 179)
  roll.write(data);
 
//The Z Axis is not used
/*
  Serial.print(" , Z = ");
  data = Wire.read();
  data += Wire.read() << 8;
  Serial.print(data);
  Serial.println();
 */

}

Thank You..
Logged

Offline Offline
Faraday Member
**
Karma: 62
Posts: 3030
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

We don't know what delays are in those roll and pitch objects you are accessing.

Try adding comments at the start of that line,  so they don't run,  and see how the speed works.
It is unlikely that the "map()"  function is the issue.

Get the millis() value at the start and end of each cycle of loop(),  and calculate the difference.
How often is loop() running ?
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 72
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Try adding comments at the start of that line,  so they don't run,  and see how the speed works.
It is unlikely that the "map()"  function is the issue.
Where i must adding comments?

Quote
How often is loop() running ?
What it mean? it's always looping..
Logged

Offline Offline
Faraday Member
**
Karma: 62
Posts: 3030
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You are complaining about slow response speed ?

One possibility is that your system is working properly,  but your expectation of how it should work,  is wrong.

The other possibility is that it is not working properly.   If you think it is "slow",  or taking too long to respond,  you
need to check for operations which your program is doing,  which are either inherently time-consuming,  or which
are subject to some long delay while they are "waiting" for something, which may or may not be happening.

There is nothing "slow" in the code you have shown us.

What we cannot see,  is the code for the roll and pitch objects,  on which you are calling functions.  Maybe the
person who wrote those functions put a long delay() in them,   to allow for the time it takes for the servo to
re-position itself.  Who knows ?  We don't.  We can't see how those objects are implemented.

My previous suggestion was to make those lines a comment,  so they don't get executed.  Put two slash characters
at the start of the line,  and the compiler will ignore it.  Like this
Code:
//  pitch.set(pwm_value);    //  this line will do nothing

See if loop() runs faster.

The other suggestion,  is to see how often loop() actually runs.  See how many milliseconds it takes.
If loop() is running in a couple of milliseconds,  then your system is not really "slow".  If loop() is
taking hundreds of milliseconds,   then there is a delay hidden in there, somewhere.

Logged

Offline Offline
Faraday Member
**
Karma: 62
Posts: 3030
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The other thing to be aware of,  is that if the device is not moving,  there won't be much acceleration,  and
you should not expect to see much movement.
What are you actually expecting this device to achieve ?
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 72
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

OK michinyon , i will try it again..
Logged

Pages: [1]   Go Up
Jump to: