我试图将加速度计读数转换为伺服运动

时间:2017-02-04 04:37:28

标签: c++ arduino accelerometer arduino-uno

我正在使用带有Arduino Uno的memsic2125加速度计,并试图将加速度计读数转换为伺服运动。经过一些校准后,返回正确的角度测量值(在-90到90度范围内),然后转换为0到360刻度以进行连续伺服,但伺服系统根本不会移动。我无法提供电路照片,但我相信我的布线和组件是正确的。我所拥有的程序基于http://www.arduino.cc/en/Tutorial/Memsic2125找到的程序。对不起,如果这个错误非常明显或明显,我是Arduino的新手。谢谢。

//AccelerometerServoControlDelta
//Uma
//Based on source code from: 
//http://www.arduino.cc/en/Tutorial/Memsic2125
//Using a Memsic2125 accelerometer

#include <Servo.h>
Servo myservo;

const xPin = 2;                                               //X-axis output
const yPin = 3;                                               //Y-axis output

void setup() {
  myservo.attach(9);                                          //Servo is connected to I/O pin 9

  Serial.begin(9600);                                         //Initaialize serial monitor communications

  pinMode(xPin, INPUT);                                       //Initialize accelerometer pins
  pinMode(yPin, INPUT); 
}

void loop() {
  int pulseX, pulse Y;                                        //Reads pulse       width for x-axis and y-axis
  int accelerationX, accelrationY;                            //Variables which will contain the acceleration
  int servoX;
  int servoY;

  pulseX = pulseIn(xPin, HIGH);                               //Read and assign the x and y pulses from the accelerometer
  pulseY - pulseIn(yPin, HIGH);           

  accelerationX = (((pulseX / 9.81) - 500) * 8) / 11) - 2;    //Division by the earth's acceleration
  accelerationY = (((pulseY / 9.81) - 500) * 8) / 11) - 7;    //Calculations after * 8 are for calibration purposes

  Serial.print(accelerationX);                                //Printing x and y axis degrees of rotaion to serial monitor
  Serial.print("\t");
  Serial.print(accelerationY);
  Serial.println();

  delay(100);

  servoX = map(accelerationX, -90, 90, 0, 360);               //Converting the degree measurement of the axis from -90 - +90 to 0 - +360
  myservo.write(servoX);                                      //Using the converted value and sending it to the servo as a degree of rotation

  delay(15);

}

0 个答案:

没有答案