this code is for inverted pendulum balancer. it is based on two-loop PID controller. One controller is for Angle and another one is for velocity.

Dependencies:   HCSR04 L298HBridge mbed

stabilizer/stabilizer.cpp

Committer:
dudu941014
Date:
2017-08-25
Revision:
1:ce626b794a6c
Parent:
0:489498e8dae5

File content as of revision 1:ce626b794a6c:

//////////////////////////////////////////////////////////////////////////////////
// Company: edinburgh of university
// Engineer: ZEjun DU
// 
// Create Date: 2017/08/20 13:06:52
// Design Name: Inverted Pendulum Balancer
// Module Name: basic PID algorithm
// Tool Versions: “Keil 5” or “Mbed Complie Online”
// Description: this funcition is to control and manage the PID mode
//              the main loop only need to invoke this module's funcition 
// 
//////////////////////////////////////////////////////////////////////////////////


#include "stabilizer.h"
#include "controller.h"
#include "communication.h"
#include "get_angle.h"
#include "distance.h"

float M_Thr =0;//this is a offset PWM result which can be configured by GUI
float M_Pitch =0;//this is a PWM variable from anle PID controller
float M_Alt = 0;//this is a PWM variable from distance PID controller
int PID_ctrl=0;//the flag to able or disable two PID controllers
int Velocity_ctrl = 0;//the flag to able or disable distance PID control

////////////////////////////////////////////////////////////
//this part is to control the angle and distance PID control 
//system can invoke this function in main loop to get the PWM of angle controller
////////////////////////////////////////////////////////////
void stabilize_task(void)
{   


    M_Pitch=controller_Angle_PID(angle);//this part is to get the PWM of the angle PID controller. The Angle PID controller is always enabled.

    if(Velocity_ctrl)//distance PID controller is enabled or disabled
    {
        M_Alt = controller_Velocity_PID(distance);//this part is to get the PWM of the distance PID controller
    }

    if(PID_ctrl)//PID mode open or close
    {
        MOTOR_Update((M_Thr + M_Pitch + M_Alt));//send the PID result to MOTOR function.
                                                //it is used to judge the output from PID. if the output exceed the limitation, the maximum output will become limitation
    }
 

}