controller_pid.c 4.83 KB
Newer Older
dusanz's avatar
dusanz committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81

#include "stabilizer.h"
#include "stabilizer_types.h"

#include "attitude_controller.h"
#include "sensfusion6.h"
#include "position_controller.h"

#include "log.h"
#include "param.h"

#define ATTITUDE_UPDATE_DT    (float)(1.0f/ATTITUDE_RATE)

static bool tiltCompensationEnabled = false;

static attitude_t attitudeDesired;
static attitude_t rateDesired;
static float actuatorThrust;

void stateControllerInit(void)
{
  attitudeControllerInit(ATTITUDE_UPDATE_DT);
  positionControllerInit();
}

bool stateControllerTest(void)
{
  bool pass = true;

  pass &= attitudeControllerTest();

  return pass;
}

void stateController(control_t *control, setpoint_t *setpoint,
                                         const sensorData_t *sensors,
                                         const state_t *state,
                                         const uint32_t tick)
{
  if (RATE_DO_EXECUTE(ATTITUDE_RATE, tick)) {
    // Rate-controled YAW is moving YAW angle setpoint
    if (setpoint->mode.yaw == modeVelocity) {
       attitudeDesired.yaw -= setpoint->attitudeRate.yaw/500.0;
      while (attitudeDesired.yaw > 180.0)
        attitudeDesired.yaw -= 360.0;
      while (attitudeDesired.yaw < -180.0)
        attitudeDesired.yaw += 360.0;
    } else {
      attitudeDesired.yaw = setpoint->attitude.yaw;
    }
  }

//  if (RATE_DO_EXECUTE(POSITION_RATE, tick)) {
//    if (setpoint->mode.z == modeAbs) {
//      positionController(&actuatorThrust, &attitudeDesired, setpoint, state);
//    } else if (setpoint->mode.z == modeVelocity) {
//      velocityController(&actuatorThrust, &attitudeDesired, setpoint, state);
//    }
//  }

  if (RATE_DO_EXECUTE(ATTITUDE_RATE, tick)) {
    // Switch between manual and automatic position control
    if (setpoint->mode.z == modeDisable) {
      actuatorThrust = setpoint->thrust;
    }
    if (setpoint->mode.x == modeDisable || setpoint->mode.y == modeDisable) {
      attitudeDesired.roll = setpoint->attitude.roll;
      attitudeDesired.pitch = setpoint->attitude.pitch;
    }

    attitudeControllerCorrectAttitudePID(state->attitude.roll, state->attitude.pitch, state->attitude.yaw,
                                attitudeDesired.roll, attitudeDesired.pitch, attitudeDesired.yaw,
                                &rateDesired.roll, &rateDesired.pitch, &rateDesired.yaw);

    if (setpoint->mode.roll == modeVelocity) {
      rateDesired.roll = setpoint->attitudeRate.roll;
    }
    if (setpoint->mode.pitch == modeVelocity) {
      rateDesired.pitch = setpoint->attitudeRate.pitch;
    }

82
83
84
85
86
87
//added for PPSSafeController to make the Onboard Controller using the external YawRate
    if (setpoint->mode.yaw == modeVelocity) {
      rateDesired.yaw = (-1) * setpoint->attitudeRate.yaw;
    }


dusanz's avatar
dusanz committed
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
    // TODO: Investigate possibility to subtract gyro drift.
    attitudeControllerCorrectRatePID(sensors->gyro.x, -sensors->gyro.y, sensors->gyro.z,
                             rateDesired.roll, rateDesired.pitch, rateDesired.yaw);

    attitudeControllerGetActuatorOutput(&control->roll,
                                        &control->pitch,
                                        &control->yaw);
    
    control->cmd1=setpoint->cmd[0];
    control->cmd2=setpoint->cmd[1];
    control->cmd3=setpoint->cmd[2];
    control->cmd4=setpoint->cmd[3];

    control->yaw = -control->yaw;
  }

//  if (tiltCompensationEnabled)
//  {
//    control->thrust = actuatorThrust / sensfusion6GetInvThrustCompensationForTilt();
//  }
//  else
//  {
//    control->thrust = actuatorThrust;
//  }

//  if (control->thrust == 0)
//  {
//    control->thrust = 0;
//    control->roll = 0;
//    control->pitch = 0;
//    control->yaw = 0;
//
//    attitudeControllerResetAllPID();
//    positionControllerResetAllPID();
//
//    // Reset the calculated YAW angle for rate control
//    attitudeDesired.yaw = state->attitude.yaw;
//  }
  
  if(setpoint->SPtype==modeMotorCmd)
  {
	      control->roll = 0;
	      control->pitch = 0;
	      control->yaw = 0;
	      
	      attitudeControllerResetAllPID();
	      positionControllerResetAllPID();
	      attitudeDesired.yaw = state->attitude.yaw;
  }
  
  if(control->cmd1==0 && control->cmd2==0 && control->cmd3==0 && control->cmd4==0)
  {
	  control->roll=0;
	  control->pitch=0;
	  control->yaw=0;
	  
      attitudeControllerResetAllPID();
      positionControllerResetAllPID();
      attitudeDesired.yaw = state->attitude.yaw;
	  
  }
}


LOG_GROUP_START(controller)
LOG_ADD(LOG_FLOAT, actuatorThrust, &actuatorThrust)
LOG_ADD(LOG_FLOAT, roll,      &attitudeDesired.roll)
LOG_ADD(LOG_FLOAT, pitch,     &attitudeDesired.pitch)
LOG_ADD(LOG_FLOAT, yaw,       &attitudeDesired.yaw)
LOG_ADD(LOG_FLOAT, rollRate,  &rateDesired.roll)
LOG_ADD(LOG_FLOAT, pitchRate, &rateDesired.pitch)
LOG_ADD(LOG_FLOAT, yawRate,   &rateDesired.yaw)
LOG_GROUP_STOP(controller)

PARAM_GROUP_START(controller)
PARAM_ADD(PARAM_UINT8, tiltComp, &tiltCompensationEnabled)
PARAM_GROUP_STOP(controller)