极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 20622|回复: 3

用dmp库和pid库和定时器堆起来的四轴程序(大四轴)

[复制链接]
发表于 2016-6-15 12:49:44 | 显示全部楼层 |阅读模式
  1. #include "Wire.h"
  2. //#include "I2Cdev.h"
  3. //#include "MPU6050.h"
  4. #include "Timer.h"
  5. #include "pid_v1.h"
  6. // I2Cdev and MPU6050 must be installed as libraries, or else the .cpp/.h files
  7. // for both classes must be in the include path of your project
  8. #include "I2Cdev.h"
  9. #include "MPU6050_6Axis_MotionApps20.h"
  10. //#include "MPU6050.h" // not necessary if using MotionApps include file

  11. // Arduino Wire library is required if I2Cdev I2CDEV_ARDUINO_WIRE implementation
  12. // is used in I2Cdev.h
  13. //#if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE
  14. //#include "Wire.h"
  15. //#endif
  16. //=================Pins===================
  17. #define TrigPin 5
  18. #define EchoPin 6
  19. #define MAE   3
  20. #define USHIRO  10
  21. #define HITARI  9
  22. #define MIGI    11
  23. //////////////////////////////////////////
  24. Timer t;//ʱ����
  25.                 // class default I2C address is 0x68
  26.                 // specific I2C addresses may be passed as a parameter here
  27.                 // AD0 low = 0x68 (default for SparkFun breakout and InvenSense evaluation board)
  28.                 // AD0 high = 0x69
  29. MPU6050 mpu;
  30. //MPU6050 mpu(0x69); // <-- use for AD0 high

  31. //=================mpu dmp==============
  32. // I2C device class (I2Cdev) demonstration Arduino sketch for MPU6050 class using DMP (MotionApps v2.0)
  33. // 6/21/2012 by Jeff Rowberg <[email][email protected][/email]>
  34. // Updates should (hopefully) always be available at https://github.com/jrowberg/i2cdevlib
  35. //
  36. // Changelog:
  37. //      2013-05-08 - added seamless Fastwire support
  38. //                 - added note about gyro calibration
  39. //      2012-06-21 - added note about Arduino 1.0.1 + Leonardo compatibility error
  40. //      2012-06-20 - improved FIFO overflow handling and simplified read process
  41. //      2012-06-19 - completely rearranged DMP initialization code and simplification
  42. //      2012-06-13 - pull gyro and accel data from FIFO packet instead of reading directly
  43. //      2012-06-09 - fix broken FIFO read sequence and change interrupt detection to RISING
  44. //      2012-06-05 - add gravity-compensated initial reference frame acceleration output
  45. //                 - add 3D math helper file to DMP6 example sketch
  46. //                 - add Euler output and Yaw/Pitch/Roll output formats
  47. //      2012-06-04 - remove accel offset clearing for better results (thanks Sungon Lee)
  48. //      2012-06-01 - fixed gyro sensitivity to be 2000 deg/sec instead of 250
  49. //      2012-05-30 - basic DMP initialization working

  50. /* ============================================
  51. I2Cdev device library code is placed under the MIT license
  52. Copyright (c) 2012 Jeff Rowberg

  53. Permission is hereby granted, free of charge, to any person obtaining a copy
  54. of this software and associated documentation files (the "Software"), to deal
  55. in the Software without restriction, including without limitation the rights
  56. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  57. copies of the Software, and to permit persons to whom the Software is
  58. furnished to do so, subject to the following conditions:

  59. The above copyright notice and this permission notice shall be included in
  60. all copies or substantial portions of the Software.

  61. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  62. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  63. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  64. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  65. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  66. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  67. THE SOFTWARE.
  68. ===============================================
  69. */

  70. /* =========================================================================
  71. NOTE: In addition to connection 3.3v, GND, SDA, and SCL, this sketch
  72. depends on the MPU-6050's INT pin being connected to the Arduino's
  73. external interrupt #0 pin. On the Arduino Uno and Mega 2560, this is
  74. digital I/O pin 2.
  75. * ========================================================================= */

  76. /* =========================================================================
  77. NOTE: Arduino v1.0.1 with the Leonardo board generates a compile error
  78. when using Serial.write(buf, len). The Teapot output uses this method.
  79. The solution requires a modification to the Arduino USBAPI.h file, which
  80. is fortunately simple, but annoying. This will be fixed in the next IDE
  81. release. For more info, see these links:

  82. http://arduino.cc/forum/index.php/topic,109987.0.html
  83. http://code.google.com/p/arduino/issues/detail?id=958
  84. * ========================================================================= */

  85. // uncomment "OUTPUT_READABLE_QUATERNION" if you want to see the actual
  86. // quaternion components in a [w, x, y, z] format (not best for parsing
  87. // on a remote host such as Processing or something though)
  88. //#define OUTPUT_READABLE_QUATERNION

  89. // uncomment "OUTPUT_READABLE_EULER" if you want to see Euler angles
  90. // (in degrees) calculated from the quaternions coming from the FIFO.
  91. // Note that Euler angles suffer from gimbal lock (for more info, see
  92. // http://en.wikipedia.org/wiki/Gimbal_lock)
  93. //#define OUTPUT_READABLE_EULER

  94. // uncomment "OUTPUT_READABLE_YAWPITCHROLL" if you want to see the yaw/
  95. // pitch/roll angles (in degrees) calculated from the quaternions coming
  96. // from the FIFO. Note this also requires gravity vector calculations.
  97. // Also note that yaw/pitch/roll angles suffer from gimbal lock (for
  98. // more info, see: http://en.wikipedia.org/wiki/Gimbal_lock)
  99. #define OUTPUT_READABLE_YAWPITCHROLL

  100. // uncomment "OUTPUT_READABLE_REALACCEL" if you want to see acceleration
  101. // components with gravity removed. This acceleration reference frame is
  102. // not compensated for orientation, so +X is always +X according to the
  103. // sensor, just without the effects of gravity. If you want acceleration
  104. // compensated for orientation, us OUTPUT_READABLE_WORLDACCEL instead.
  105. //#define OUTPUT_READABLE_REALACCEL

  106. // uncomment "OUTPUT_READABLE_WORLDACCEL" if you want to see acceleration
  107. // components with gravity removed and adjusted for the world frame of
  108. // reference (yaw is relative to initial orientation, since no magnetometer
  109. // is present in this case). Could be quite handy in some cases.
  110. //#define OUTPUT_READABLE_WORLDACCEL

  111. // uncomment "OUTPUT_TEAPOT" if you want output that matches the
  112. // format used for the InvenSense teapot demo
  113. //#define OUTPUT_TEAPOT

  114. #define INTERRUPT_PIN 2  // use pin 2 on Arduino Uno & most boards
  115. #define LED_PIN 13 // (Arduino is 13, Teensy is 11, Teensy++ is 6)
  116. bool blinkState = false;

  117. // MPU control/status vars
  118. bool dmpReady = false;  // set true if DMP init was successful
  119. uint8_t mpuIntStatus;   // holds actual interrupt status byte from MPU
  120. uint8_t devStatus;      // return status after each device operation (0 = success, !0 = error)
  121. uint16_t packetSize;    // expected DMP packet size (default is 42 bytes)
  122. uint16_t fifoCount;     // count of all bytes currently in FIFO
  123. uint8_t fifoBuffer[64]; // FIFO storage buffer

  124.                                                 // orientation/motion vars
  125. Quaternion q;           // [w, x, y, z]         quaternion container
  126. VectorInt16 aa;         // [x, y, z]            accel sensor measurements
  127. VectorInt16 aaReal;     // [x, y, z]            gravity-free accel sensor measurements
  128. VectorInt16 aaWorld;    // [x, y, z]            world-frame accel sensor measurements
  129. VectorFloat gravity;    // [x, y, z]            gravity vector
  130. float euler[3];         // [psi, theta, phi]    Euler angle container
  131. float ypr[3];           // [yaw, pitch, roll]   yaw/pitch/roll container and gravity vector

  132.                                                 // packet structure for InvenSense teapot demo
  133. uint8_t teapotPacket[14] = { '$', 0x02, 0,0, 0,0, 0,0, 0,0, 0x00, 0x00, '\r', '\n' };
  134. double angleAy;// , gyroGx;
  135. double angleAx;// , gyroGy; //&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#317;&#502;&#547;&#65533;&#65533;&#65533;x&#65533;&#65533;&#65533;н&#483;&#65533;&#65533;&#893;&#65533;&#65533;&#1654;&#65533;
  136.                            //MPU6050 accelgyro;//&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;
  137.                            //int16_t ax, ay, az, gx, gy, gz;//&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#1325;&#700;&#65533;&#65533;&#65533;&#65533; 3&#65533;&#65533;&#65533;&#65533;&#65533;&#1654;&#65533;+3&#65533;&#65533;&#65533;&#65533;&#65533;&#1654;&#65533;



  138.                            // ================================================================
  139.                            // ===               INTERRUPT DETECTION ROUTINE                ===
  140.                            // ================================================================
  141. volatile bool mpuInterrupt = false;     // indicates whether MPU interrupt pin has gone high
  142. void dmpDataReady() {
  143.         mpuInterrupt = true;
  144. }
  145. void mpu_Setup() {
  146.         // join I2C bus (I2Cdev library doesn't do this automatically)
  147. #if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE
  148.         Wire.begin();
  149.         Wire.setClock(400000); // 400kHz I2C clock. Comment this line if having compilation difficulties
  150. #elif I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_FASTWIRE
  151.         Fastwire::setup(400, true);
  152. #endif

  153.         // initialize serial communication
  154.         // (115200 chosen because it is required for Teapot Demo output, but it's
  155.         // really up to you depending on your project)
  156.         Serial.begin(115200);
  157.         while (!Serial); // wait for Leonardo enumeration, others continue immediately

  158.                                          // NOTE: 8MHz or slower host processors, like the Teensy @ 3.3v or Ardunio
  159.                                          // Pro Mini running at 3.3v, cannot handle this baud rate reliably due to
  160.                                          // the baud timing being too misaligned with processor ticks. You must use
  161.                                          // 38400 or slower in these cases, or use some kind of external separate
  162.                                          // crystal solution for the UART timer.

  163.                                          // initialize device
  164.         Serial.println(F("Initializing I2C devices..."));
  165.         mpu.initialize();
  166.         pinMode(INTERRUPT_PIN, INPUT);

  167.         // verify connection
  168.         Serial.println(F("Testing device connections..."));
  169.         Serial.println(mpu.testConnection() ? F("MPU6050 connection successful") : F("MPU6050 connection failed"));

  170.         // wait for ready
  171.         Serial.println(F("\nSend any character to begin DMP programming and demo: "));
  172.         while (Serial.available() && Serial.read()); // empty buffer
  173.         while (!Serial.available());                 // wait for data
  174.         while (Serial.available() && Serial.read()); // empty buffer again

  175.                                                                                                  // load and configure the DMP
  176.         Serial.println(F("Initializing DMP..."));
  177.         devStatus = mpu.dmpInitialize();

  178.         // supply your own gyro offsets here, scaled for min sensitivity
  179.         mpu.setXGyroOffset(220);
  180.         mpu.setYGyroOffset(76);
  181.         mpu.setZGyroOffset(-85);
  182.         mpu.setZAccelOffset(1788); // 1688 factory default for my test chip

  183.                                                            // make sure it worked (returns 0 if so)
  184.         if (devStatus == 0) {
  185.                 // turn on the DMP, now that it's ready
  186.                 Serial.println(F("Enabling DMP..."));
  187.                 mpu.setDMPEnabled(true);

  188.                 // enable Arduino interrupt detection
  189.                 Serial.println(F("Enabling interrupt detection (Arduino external interrupt 0)..."));
  190.                 attachInterrupt(digitalPinToInterrupt(INTERRUPT_PIN), dmpDataReady, RISING);
  191.                 mpuIntStatus = mpu.getIntStatus();

  192.                 // set our DMP Ready flag so the main loop() function knows it's okay to use it
  193.                 Serial.println(F("DMP ready! Waiting for first interrupt..."));
  194.                 dmpReady = true;

  195.                 // get expected DMP packet size for later comparison
  196.                 packetSize = mpu.dmpGetFIFOPacketSize();
  197.         }
  198.         else {
  199.                 // ERROR!
  200.                 // 1 = initial memory load failed
  201.                 // 2 = DMP configuration updates failed
  202.                 // (if it's going to break, usually the code will be 1)
  203.                 Serial.print(F("DMP Initialization failed (code "));
  204.                 Serial.print(devStatus);
  205.                 Serial.println(F(")"));
  206.         }

  207.         // configure LED for output
  208.         pinMode(LED_PIN, OUTPUT);
  209. }
  210. void getData() {
  211.         // if programming failed, don't try to do anything
  212.         if (!dmpReady) return;

  213.         // wait for MPU interrupt or extra packet(s) available
  214.         while (!mpuInterrupt && fifoCount < packetSize) {
  215.                 // other program behavior stuff here
  216.                 // .
  217.                 // .
  218.                 // .
  219.                 // if you are really paranoid you can frequently test in between other
  220.                 // stuff to see if mpuInterrupt is true, and if so, "break;" from the
  221.                 // while() loop to immediately process the MPU data
  222.                 // .
  223.                 // .
  224.                 // .
  225.         }

  226.         // reset interrupt flag and get INT_STATUS byte
  227.         mpuInterrupt = false;
  228.         mpuIntStatus = mpu.getIntStatus();

  229.         // get current FIFO count
  230.         fifoCount = mpu.getFIFOCount();

  231.         // check for overflow (this should never happen unless our code is too inefficient)
  232.         if ((mpuIntStatus & 0x10) || fifoCount == 1024) {
  233.                 // reset so we can continue cleanly
  234.                 mpu.resetFIFO();
  235.                 Serial.println(F("FIFO overflow!"));

  236.                 // otherwise, check for DMP data ready interrupt (this should happen frequently)
  237.         }
  238.         else if (mpuIntStatus & 0x02) {
  239.                 // wait for correct available data length, should be a VERY short wait
  240.                 while (fifoCount < packetSize) fifoCount = mpu.getFIFOCount();

  241.                 // read a packet from FIFO
  242.                 mpu.getFIFOBytes(fifoBuffer, packetSize);

  243.                 // track FIFO count here in case there is > 1 packet available
  244.                 // (this lets us immediately read more without waiting for an interrupt)
  245.                 fifoCount -= packetSize;


  246.                 //#ifdef OUTPUT_READABLE_YAWPITCHROLL
  247.                 // display Euler angles in degrees
  248.                 mpu.dmpGetQuaternion(&q, fifoBuffer);
  249.                 mpu.dmpGetGravity(&gravity, &q);
  250.                 mpu.dmpGetYawPitchRoll(ypr, &q, &gravity);
  251.                 //Serial.print("ypr\t");
  252.                 //Serial.print(ypr[0] * 180 / M_PI);
  253.                 //Serial.print("\t");
  254.                 //Serial.print(ypr[1] * 180 / M_PI);
  255.                 angleAx = ypr[1] * 180 / M_PI - 0.25;
  256.                 //Serial.print("\t");
  257.                 //Serial.println(ypr[2] * 180 / M_PI);
  258.                 angleAy = ypr[2] * 180 / M_PI - 0.7;
  259.                 //#endif


  260.                 // blink LED to indicate activity
  261.                 blinkState = !blinkState;
  262.                 digitalWrite(LED_PIN, blinkState);
  263.         }
  264. }


  265. //==================PID================
  266. int pid_Sample_Time = 10;
  267. double x_mae_pid_out, x_ushiro_pid_out, y_migi_pid_out, y_hitari_pid_out;
  268. //double Px,Ix,Dx,Py,Iy,Dy;
  269. double Px = 0;
  270. double Ix = 0;
  271. double Dx = 0;

  272. //double Px_ = 0.5;
  273. //double Ix_ = 0;
  274. //double Dx_ = 0;

  275. double Py = 0;
  276. double Iy = 0;
  277. double Dy = 0;

  278. //double Py_ = 0.5;
  279. //double Iy_ = 0;
  280. //double Dy_ = 0;

  281. double x_set_angle = 0;
  282. double y_set_angle = 0;


  283. PID x_mae_PID(&angleAx, &x_mae_pid_out, &x_set_angle, Px, Ix, Dx, DIRECT);
  284. //PID x_ushiro_PID(&angleAx, &x_ushiro_pid_out, &x_set_angle, Py_, Iy_, Dy_, REVERSE);
  285. PID y_hitari_PID(&angleAy, &y_hitari_pid_out, &y_set_angle, Py, Iy, Dy, DIRECT);
  286. //PID y_migi_PID(&angleAy, &y_migi_pid_out, &y_set_angle, Py_, Iy_, Dy_, REVERSE);

  287. void pid_Config(int _limit) {
  288.         x_mae_PID.SetMode(AUTOMATIC);//pid mode
  289.         //x_ushiro_PID.SetMode(AUTOMATIC);//pid mode
  290.         x_mae_PID.SetOutputLimits(0, _limit);//range
  291.         //x_ushiro_PID.SetOutputLimits(0, out_limit);//range
  292.         //x_ushiro_PID.SetSampleTime(pid_Sample_Time);
  293.         x_mae_PID.SetSampleTime(pid_Sample_Time);
  294.         y_hitari_PID.SetMode(AUTOMATIC);//pid mode
  295.         //y_migi_PID.SetMode(AUTOMATIC);//pid mode
  296.         y_hitari_PID.SetOutputLimits(0, _limit);//range
  297.         //y_migi_PID.SetOutputLimits(0, out_limit);//range
  298.         y_hitari_PID.SetSampleTime(pid_Sample_Time);
  299.         //y_migi_PID.SetSampleTime(pid_Sample_Time);
  300. }
  301. //===================PWM============
  302. int val1;
  303. int unlock = 0;
  304. int myangle1 = 0;
  305. int out_limit = 40;
  306. int basic_Power = 0;
  307. int motor_PWM[4] = { 0,0,0,0 };
  308. int x_ushiro_fix = 0.7;
  309. void servopulse(int val1)//&#65533;&#65533;&#65533;&#65533;&#1211;&#65533;&#65533;&#65533;&#65533;&#65533;庯&#65533;&#65533;
  310. {
  311.         myangle1 = map(val1, 0, 180, 500, 2480);
  312.         digitalWrite(MAE, HIGH);//&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#1279;&#1717;&#65533;&#445;&#65533;&#65533;&#65533;&#65533;
  313.         digitalWrite(MIGI, HIGH);
  314.         digitalWrite(USHIRO, HIGH);
  315.         digitalWrite(HITARI, HIGH);
  316.         delayMicroseconds(myangle1);//&#65533;&#65533;&#689;&#65533;&#65533;&#65533;&#65533;&#1461;&#65533;&#65533;&#930;&#65533;&#65533;&#65533;&#65533;
  317.         digitalWrite(MAE, LOW);//&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#1279;&#1717;&#65533;&#445;&#65533;&#65533;&#65533;&#65533;
  318.         digitalWrite(MIGI, LOW);
  319.         digitalWrite(USHIRO, LOW);
  320.         digitalWrite(HITARI, LOW);
  321.         delay(20 - val1 / 1000);
  322. }
  323. void ESC_Prepare(int ESC_mode) {
  324.         //&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#757;&#65533;&#65533;&#65533;飬&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;г&#65533;&#689;&#65533;&#65533;&#1211;&#65533;&#65533;&#700;&#65533;&#65533;&#1194;&#65533;&#65533;&#1187;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#2037;&#14435;i<=110&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;2&#65533;&para;&#65533;
  325.         if (ESC_mode == 0) {//&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;г&#803;&#65533;
  326.                 for (int i = 0; i <= 110; i++)
  327.                 {
  328.                         servopulse(150);//&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;庯&#65533;&#65533;
  329.                 }
  330.         }
  331.         //&#65533;&#565;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;B-B&#65533;&#65533;&#65533;&#65533;&#65533;&#932414;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#932660;&#65533;&#65533;&#382;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#739;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#372;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#885;&#65533;
  332.         for (int i = 0; i <= 55; i++)
  333.         {
  334.                 servopulse(20);//&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;庯&#65533;&#65533;
  335.         }

  336.         //&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#702;&#65533;&#932670;&#895;&#65533;&#65533;&#1343;&#65533;&#65533;&#437;&#65533;&#65533;&#65533;&#1514;&#65533;&#65533;&#65533;&#65533;

  337.         delay(1000);
  338. }
  339. void motor_Prepare() {
  340.         pinMode(MAE, OUTPUT);
  341.         pinMode(USHIRO, OUTPUT);
  342.         pinMode(HITARI, OUTPUT);
  343.         pinMode(MIGI, OUTPUT);
  344.         //&#65533;&#65533;&#65533;&#65533;analogWrite()&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;PWM&#65533;&#65533;&#437;&#65533;&#65533;&#938; 122Hz
  345.         TCCR1B = TCCR1B & 0xF8 | 4; // 122.549
  346.         TCCR2B = TCCR2B & 0xF8 | 6; //122.549
  347.                                                                 //&#65533;&#65533;&#65533;&#65533;analogWrite()&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;PWM&#65533;&#65533;&#437;&#65533;&#65533;&#938; defaultValue
  348.                                                                 //     TCCR1B =  TCCR1B & 0xF8 | 3; /// 490.196
  349.                                                                 //   TCCR2B =  TCCR2B & 0xF8 | 4; ///490.196
  350. }
  351. void PWM_Update() {
  352.         if (angleAx>=0){
  353.                 x_mae_PID.SetControllerDirection(REVERSE);
  354.                 x_mae_PID.Compute();
  355.                 motor_PWM[0] = basic_Power - x_mae_pid_out; //x +    mae
  356.                 motor_PWM[1] = basic_Power + x_mae_pid_out-x_ushiro_fix;//x -               ushiro
  357.         }
  358.         else {
  359.                 x_mae_PID.SetControllerDirection(DIRECT);
  360.                 x_mae_PID.Compute();
  361.                 motor_PWM[0] = basic_Power + x_mae_pid_out; //x +    mae
  362.                 motor_PWM[1] = basic_Power - x_mae_pid_out+x_ushiro_fix;//x -               ushiro
  363.         }
  364.         if (angleAy>=0) {
  365.                 y_hitari_PID.SetControllerDirection(REVERSE);
  366.                 y_hitari_PID.Compute();       
  367.                 motor_PWM[2] = basic_Power - y_hitari_pid_out;//y +                            hitari       
  368.                 motor_PWM[3] = basic_Power + y_hitari_pid_out;//y -                                migi
  369.         }
  370.         else {
  371.                 y_hitari_PID.SetControllerDirection(DIRECT);
  372.                 y_hitari_PID.Compute();
  373.                 motor_PWM[2] = basic_Power + y_hitari_pid_out;//y +                            hitari       
  374.                 motor_PWM[3] = basic_Power - y_hitari_pid_out;//y -                                migi
  375.         }
  376. //calc
  377.         //x_ushiro_PID.Compute();//calc
  378.         //y_migi_PID.Compute();
  379.                                                                                                 //Serial.print(motor_PWM[0]); Serial.println(',');
  380.                                                                                                 //        Serial.println(motor_PWM[1]);
  381.         for (size_t i = 0; i < 4; i++)
  382.         {
  383.                 if (motor_PWM[i] < 0)
  384.                 {
  385.                         motor_PWM[i] = 0;//&#65533;&#65533;&#65533;&#65533;&#65533;&#1975;&#65533;
  386.                 }
  387.                 else if (motor_PWM[i] > 50)
  388.                 {
  389.                         motor_PWM[i] = 50;//&#65533;&#65533;&#65533;&#65533;&#65533;&#1975;&#65533;
  390.                 }
  391.         }

  392.         if (unlock)
  393.         {
  394.                
  395.                 analogWrite(MAE, (int)motor_PWM[0]);
  396.                 analogWrite(USHIRO, (int)motor_PWM[1]);
  397.                 analogWrite(HITARI, (int)motor_PWM[2]);
  398.                 analogWrite(MIGI, (int)motor_PWM[3]);
  399.         }
  400.         else
  401.         {
  402.                 basic_Power = 0;
  403.                 analogWrite(MAE, 0);
  404.                 analogWrite(USHIRO, 0);
  405.                 analogWrite(HITARI, 0);
  406.                 analogWrite(MIGI, 0);
  407.         }
  408.         }
  409. //------------------DEBUG----------------
  410. int data = 0;
  411. int debug_Time = 500;
  412. void bluetooth_RC()
  413. {
  414.         int flag = 0;
  415.         while (Serial.available() > 0) {
  416.                 data = Serial.parseInt();
  417.                 if (Serial.read() == 'X') {
  418.                         switch (flag)
  419.                         {
  420.                         case 0: {
  421.                                 //    vrx = data;
  422.                                 flag = 1;
  423.                                   Px = (double)(data / 1000.0);
  424.                                 //Serial.println(data);
  425.                                 break;
  426.                         }
  427.                         case 1: {
  428.                                  Dx = (double )(data / 1000.0);
  429.                                 flag = 2;
  430.                                 //  Serial.println(data);
  431.                                 break;
  432.                         }
  433.                         case 2: {
  434.                                 basic_Power = data;
  435.                                 flag = 3;
  436.                                 // Serial.println(data);
  437.                                 break;
  438.                         }
  439.                         case 3: {
  440.                                 unlock = data;//Py = data/100;
  441.                                 flag = 4;
  442.                                 break;
  443.                         }
  444.                         case 4: {
  445.                                 //int Dx = data / 100.0;
  446.                                 if (data)x_mae_PID.SetTunings(Px, Ix, Dx);
  447.                                 flag = 5;
  448.                                 break;
  449.                         }
  450.                         case 5: {
  451.                                 Ix =data/1000.0;
  452.                                 x_mae_PID.SetTunings(Px, Ix, Dx);
  453.                                 flag = 0;
  454.                                 break;
  455.                         }
  456.                                         //                case 6: {
  457.                                         //                        Px = data/100.0;
  458.                                         //                        flag = 7;
  459.                                         //                        break;
  460.                                         //                }
  461.                                         //                case 7: {
  462.                                         //                        Ix = data/100.0;
  463.                                         //                        flag = 8;
  464.                                         //                        break;
  465.                                         //                }
  466.                                         //                case 8: {
  467.                                         //                        Dx = data/100.0;
  468.                                         //                        flag = 0;
  469.                                         //                        break;
  470.                                         //                }
  471.                         }
  472.                 }
  473.         }
  474. }
  475. //=============================
  476. void printout()
  477. {
  478.         Serial.print(angleAx); Serial.print(',');
  479.         Serial.print(angleAy); Serial.print(',');
  480.         Serial.print(Px); Serial.print(',');
  481.         Serial.print(Dx); Serial.println(',');
  482.         Serial.print(motor_PWM[0]); Serial.println(',');
  483.         Serial.print(motor_PWM[1]); Serial.println("  ");
  484.         Serial.print(motor_PWM[2]); Serial.println(',');
  485.         Serial.print(motor_PWM[3]); Serial.println("  ");
  486.         //Serial.print(angle2);Serial.print(',');
  487.         // Serial.print(gx/131.00);Serial.print(',');
  488.         // Serial.println(angle);//Serial.print(',');

  489.         //   Serial.println(Output);
  490. }
  491. void setup() {
  492.         pid_Config(20);//pid init
  493.         motor_Prepare();
  494.         ESC_Prepare(0);
  495.         int tickEvent1 = t.every(5, getData); //&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#1460;&#65533;&#65533;&#65533;&#1338;&#65533;timeChange&#65533;&#65533;&#65533;&#65533;&#1460;&#65533;л&#1589;&#65533;&#65533;&#65533;&#65533;&#65533;getangle
  496.         int tickEvent2 = t.every(50, printout); //&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#1460;&#65533;&#65533;&#65533;&#1338;&#65533;50&#65533;&#65533;&#65533;&#65533;&#1460;&#65533;л&#1589;&#65533;&#65533;&#65533;&#65533;&#65533;printout&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;
  497.         int tickEvent3 = t.every(pid_Sample_Time, PWM_Update);//1oms &#1211;&#65533;&#65533;pid
  498.         int tickEvent4 = t.every(debug_Time, bluetooth_RC);
  499.         mpu_Setup();
  500. }
  501. void loop() {
  502.         t.update();//&#689;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#1013;&#883;&#65533;&#65533;&#65533;&#65533;
  503.                            //getData();
  504.                            //printout();
  505. }
复制代码

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
回复

使用道具 举报

 楼主| 发表于 2016-6-15 12:52:38 | 显示全部楼层
用蓝牙调试的,
还有不知道为什么发上来注释就变成();//&#689;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#1013;&#883;&#65533;&#65533;&#6553
3;&#65533;这样了
回复 支持 反对

使用道具 举报

发表于 2016-6-16 14:09:30 | 显示全部楼层
AilikeZ 发表于 2016-6-15 12:52
用蓝牙调试的,
还有不知道为什么发上来注释就变成();//&#689;&#65533;&#65533;&#65533;&#65533;&#65533;& ...

你的这套程序调试通了没
回复 支持 反对

使用道具 举报

发表于 2016-8-8 20:18:48 | 显示全部楼层
大神,为什么编译会出现这种情况

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则 需要先绑定手机号

Archiver|联系我们|极客工坊

GMT+8, 2024-4-20 19:01 , Processed in 0.041023 second(s), 19 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表