This is a one axis gimbal control program that takes roll angle from an IMU and moves the gimbal brushless motor accordingly.

Dependencies:   MPU6050 brushlessController_TB6612FNG ledControl2 mbed

main.cpp

Committer:
BaserK
Date:
2015-07-21
Revision:
4:a041b7b5720b
Parent:
3:065a064b3453
Child:
5:477eaa33eff5

File content as of revision 4:a041b7b5720b:

/*   Brushless gimbal controller with IMU (MPU050)
*
*    @author: Baser Kandehir 
*    @date: July 17, 2015
*    @license: MIT license
*     
*   Copyright (c) 2015, Baser Kandehir, baser.kandehir@ieee.metu.edu.tr
*
*   Permission is hereby granted, free of charge, to any person obtaining a copy
*   of this software and associated documentation files (the "Software"), to deal
*   in the Software without restriction, including without limitation the rights
*   to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
*   copies of the Software, and to permit persons to whom the Software is
*   furnished to do so, subject to the following conditions:
*
*   The above copyright notice and this permission notice shall be included in
*   all copies or substantial portions of the Software.
*
*   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
*   IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
*   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
*   AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
*   LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
*   OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
*   THE SOFTWARE.
*  
*    @description of the program: 
*    
*    This a one axis gimbal control program that takes roll angle from an IMU 
*    and  moves the gimbal brushless motor accordingly. Program is written with
*    PID algorithm but it uses only P for the time being and response is good
*    enough. Complete PID algorithm can be written by modifying gimbalPID function
*    and PID constants. 
*
*    Microcontroller: LPC1768  
*    IMU: MPU6050
*    Motor driver: 2x TB6612FNG
*
*   Note: For any mistakes or comments, please contact me.
*/

#include "mbed.h"
#include "MPU6050.h"
#include "ledControl.h"
#include "brushlessController_TB6612FNG.h"

Serial pc(USBTX, USBRX);       // Create terminal link
MPU6050 mpu6050;               // mpu6050 object from MPU6050 classs
Ticker toggler1;               // Ticker for led toggling
Ticker filter;                 // Ticker for periodic call to compFilter funcçs 
Ticker gimbal;                 // Periodic routine for PID control of gimbal system

/* Function prototypes */
void toggle_led1();
void toggle_led2();
void compFilter();
void gimbalPID();

float pitchAngle = 0;
float rollAngle = 0;

/* Variables to be used in gimbalPID funct. */
float Kp = 1;
float Ki = 0; 
float Kd = 0; 
float set_point = -45;         // which angle camera should stay
float errorMargin = 2;         // error margin in degress
float proportional = 0;
float last_proportional =0;
float integral = 0;
float derivative = 0;
float errorPID = 0;            // error is already declared at mbed libraries

int main()
{
    pc.baud(9600);                               // baud rate: 9600
    i2c.frequency(400000);                       // fast i2c: 400 kHz
    mpu6050.whoAmI();                            // Communication test: WHO_AM_I register reading 
    mpu6050.calibrate(accelBias,gyroBias);       // Calibrate MPU6050 and load biases into bias registers
    pc.printf("Calibration is completed. \r\n"); 
    mpu6050.init();                              // Initialize the sensor
    pc.printf("MPU6050 is initialized for operation.. \r\n\r\n");
    
    filter.attach(&compFilter, 0.005);       // Call the complementaryFilter func. every 5 ms (200 Hz sampling period)  
    while(1)
    { 
        if(abs(errorPID) < 10)                  
            gimbal.attach(&gimbalPID,  0.005);     // Fine tuning
        else    
            gimbal.attach(&gimbalPID,  0.001);     // Coarse tuning    
        
        pc.printf("%.1f,%.1f\r\n",rollAngle,pitchAngle);
        wait_ms(40);
    }    
}

void toggle_led1() {ledToggle(1);}
void toggle_led2() {ledToggle(2);}

/* This function is created to avoid address error that caused from Ticker.attach func */ 
void compFilter() {mpu6050.complementaryFilter(&pitchAngle, &rollAngle);}

void gimbalPID()
{
    bool dir;             // direction of movement
 
    proportional = set_point - rollAngle;        
    integral += proportional; 
    derivative = proportional - last_proportional;
    last_proportional = proportional;
    
    errorPID = (Kp * proportional) + (Ki * integral) + (Kd * derivative); 
    (errorPID > 0)?(dir = 1):(dir = 0);
   
    if(abs(errorPID) > errorMargin)  // Within error margin motor wont move. This is for stabilizing the gimbal system   
        brushlessControl(dir, 0);    // No need for a delay time because gimbalPID function is periodic
}