Subversion Repositories FlightCtrl

Rev

Rev 844 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 844 Rev 898
Line 116... Line 116...
116
volatile int16_t  DiffPitch, DiffRoll;
116
volatile int16_t  DiffPitch, DiffRoll;
Line 117... Line 117...
117
 
117
 
Line 118... Line 118...
118
int16_t  Poti1 = 0, Poti2 = 0, Poti3 = 0, Poti4 = 0, Poti5 = 0, Poti6 = 0, Poti7 = 0, Poti8 = 0;
118
int16_t  Poti1 = 0, Poti2 = 0, Poti3 = 0, Poti4 = 0, Poti5 = 0, Poti6 = 0, Poti7 = 0, Poti8 = 0;
119
 
119
 
Line 120... Line 120...
120
// setpoints for motors
120
// setpoints for motors
121
volatile uint8_t Motor_Front, Motor_Rear, Motor_Right, Motor_Left;
121
volatile uint8_t Motor_FrontLeft, Motor_FrontRight, Motor_RearLeft, Motor_RearRight, Motor_Right, Motor_Left;
122
 
122
 
123
// stick values derived by rc channels readings
123
// stick values derived by rc channels readings
Line 357... Line 357...
357
/************************************************************************/
357
/************************************************************************/
358
void SendMotorData(void)
358
void SendMotorData(void)
359
{
359
{
360
    if(MOTOR_OFF || !MotorsOn)
360
    if(MOTOR_OFF || !MotorsOn)
361
    {
361
    {
362
        Motor_Rear = 0;
362
        Motor_RearLeft = 0;
-
 
363
                Motor_RearRight = 0;
363
        Motor_Front = 0;
364
        Motor_FrontLeft = 0;
-
 
365
                Motor_FrontRight = 0;
364
        Motor_Right = 0;
366
        Motor_Right = 0;
365
        Motor_Left = 0;
367
        Motor_Left = 0;
366
        if(MotorTest[0]) Motor_Front = MotorTest[0];
368
        if(MotorTest[0]) Motor_FrontLeft = Motor_FrontRight = MotorTest[0];
367
        if(MotorTest[1]) Motor_Rear  = MotorTest[1];
369
        if(MotorTest[1]) Motor_RearLeft  = Motor_RearRight  = MotorTest[1];
368
        if(MotorTest[2]) Motor_Left  = MotorTest[2];
370
        if(MotorTest[2]) Motor_Left  = MotorTest[2];
369
        if(MotorTest[3]) Motor_Right = MotorTest[3];
371
        if(MotorTest[3]) Motor_Right = MotorTest[3];
370
     }
372
     }
Line 371... Line 373...
371
 
373
 
372
    //DebugOut.Analog[12] = Motor_Front;
374
    DebugOut.Analog[12] = Motor_FrontLeft;
-
 
375
    DebugOut.Analog[13] = Motor_RearRight;
-
 
376
    DebugOut.Analog[14] = Motor_FrontRight;
373
    //DebugOut.Analog[13] = Motor_Rear;
377
    DebugOut.Analog[15] = Motor_RearLeft;
374
    //DebugOut.Analog[14] = Motor_Left;
378
    DebugOut.Analog[16] = Motor_Left;
375
    //DebugOut.Analog[15] = Motor_Right;
379
    DebugOut.Analog[17] = Motor_Right;
376
 
380
       
377
    //Start I2C Interrupt Mode
381
    //Start I2C Interrupt Mode
378
    twi_state = 0;
382
    twi_state = 0;
379
    motor = 0;
383
    motor = 0;
380
    I2C_Start();
384
    I2C_Start();
Line 422... Line 426...
422
/*  MotorControl                                                        */
426
/*  MotorControl                                                        */
423
/************************************************************************/
427
/************************************************************************/
424
void MotorControl(void)
428
void MotorControl(void)
425
{
429
{
426
        int16_t MotorValue, pd_result, h, tmp_int;
430
        int16_t MotorValue, pd_result, h, tmp_int;
427
        int16_t YawMixFraction, ThrustMixFraction;
431
        int16_t YawMixFraction, ThrustMixFraction, PitchMixFraction, RollMixFraction;
428
        static int32_t SumPitch = 0, SumRoll = 0;
432
        static int32_t SumPitch = 0, SumRoll = 0;
429
        static int32_t SetPointYaw = 0;
433
        static int32_t SetPointYaw = 0;
430
        static int32_t IntegralErrorPitch = 0;
434
        static int32_t IntegralErrorPitch = 0;
431
        static int32_t IntegralErrorRoll = 0;
435
        static int32_t IntegralErrorRoll = 0;
432
        static uint16_t RcLostTimer;
436
        static uint16_t RcLostTimer;
Line 885... Line 889...
885
                        // deviation of gyro pitch integral (IntegralPitch is corrected by averaged acc sensor)
889
                        // deviation of gyro pitch integral (IntegralPitch is corrected by averaged acc sensor)
886
                        IntegralErrorRoll = IntegralRoll2 - IntegralRoll;
890
                        IntegralErrorRoll = IntegralRoll2 - IntegralRoll;
887
                        Reading_IntegralGyroRoll2 -= IntegralErrorRoll;
891
                        Reading_IntegralGyroRoll2 -= IntegralErrorRoll;
Line 888... Line 892...
888
 
892
 
889
 
893
 
890
                        DebugOut.Analog[17] = IntegralAccPitch / 26;
894
                        // DebugOut.Analog[17] = IntegralAccPitch / 26;
891
                        DebugOut.Analog[18] = IntegralAccRoll / 26;
895
                        DebugOut.Analog[18] = IntegralAccRoll / 26;
892
                        DebugOut.Analog[19] = IntegralErrorPitch;// / 26;
896
                        DebugOut.Analog[19] = IntegralErrorPitch;// / 26;
893
                        DebugOut.Analog[20] = IntegralErrorRoll;// / 26;
897
                        DebugOut.Analog[20] = IntegralErrorRoll;// / 26;
Line 1089... Line 1093...
1089
                DebugOut.Analog[6]  = (Reading_Integral_Top / 512);
1093
                DebugOut.Analog[6]  = (Reading_Integral_Top / 512);
1090
                DebugOut.Analog[8]  = CompassHeading;
1094
                DebugOut.Analog[8]  = CompassHeading;
1091
                DebugOut.Analog[9]  = UBat;
1095
                DebugOut.Analog[9]  = UBat;
1092
                DebugOut.Analog[10] = RC_Quality;
1096
                DebugOut.Analog[10] = RC_Quality;
1093
                //DebugOut.Analog[11] = RC_Quality;
1097
                //DebugOut.Analog[11] = RC_Quality;
1094
                DebugOut.Analog[16] = Mean_AccTop;
1098
                //DebugOut.Analog[16] = Mean_AccTop;
Line 1095... Line 1099...
1095
 
1099
 
1096
                /*    DebugOut.Analog[16] = motor_rx[0];
1100
                /*    DebugOut.Analog[16] = motor_rx[0];
1097
                DebugOut.Analog[17] = motor_rx[1];
1101
                DebugOut.Analog[17] = motor_rx[1];
1098
                DebugOut.Analog[18] = motor_rx[2];
1102
                DebugOut.Analog[18] = motor_rx[2];
Line 1197... Line 1201...
1197
        // limit thrust to parameter setting
1201
        // limit thrust to parameter setting
1198
        if(ThrustMixFraction > ParamSet.Trust_Max - 20) ThrustMixFraction = ParamSet.Trust_Max - 20;
1202
        if(ThrustMixFraction > ParamSet.Trust_Max - 20) ThrustMixFraction = ParamSet.Trust_Max - 20;
1199
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1203
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1200
// + Mixer and PI-Controller
1204
// + Mixer and PI-Controller
1201
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1205
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-
 
1206
 
1202
        DebugOut.Analog[7] = ThrustMixFraction;
1207
        DebugOut.Analog[7] = ThrustMixFraction;
1203
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1208
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1204
// Yaw-Fraction
1209
// Yaw-Fraction
1205
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1210
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1206
    YawMixFraction = Reading_GyroYaw - SetPointYaw;     // yaw controller
1211
    YawMixFraction = Reading_GyroYaw - SetPointYaw;     // yaw controller
Line 1209... Line 1214...
1209
    if(YawMixFraction > (ThrustMixFraction / 2)) YawMixFraction = ThrustMixFraction / 2;
1214
    if(YawMixFraction > (ThrustMixFraction / 2)) YawMixFraction = ThrustMixFraction / 2;
1210
    if(YawMixFraction < -(ThrustMixFraction / 2)) YawMixFraction = -(ThrustMixFraction / 2);
1215
    if(YawMixFraction < -(ThrustMixFraction / 2)) YawMixFraction = -(ThrustMixFraction / 2);
1211
    if(YawMixFraction > ((ParamSet.Trust_Max - ThrustMixFraction))) YawMixFraction = ((ParamSet.Trust_Max - ThrustMixFraction));
1216
    if(YawMixFraction > ((ParamSet.Trust_Max - ThrustMixFraction))) YawMixFraction = ((ParamSet.Trust_Max - ThrustMixFraction));
1212
    if(YawMixFraction < -((ParamSet.Trust_Max - ThrustMixFraction))) YawMixFraction = -((ParamSet.Trust_Max - ThrustMixFraction));
1217
    if(YawMixFraction < -((ParamSet.Trust_Max - ThrustMixFraction))) YawMixFraction = -((ParamSet.Trust_Max - ThrustMixFraction));
1213
    if(ThrustMixFraction < 20) YawMixFraction = 0;
1218
    if(ThrustMixFraction < 20) YawMixFraction = 0;
-
 
1219
       
1214
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1220
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1215
// Pitch-Axis
1221
// Pitch-Axis
1216
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1222
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1217
    DiffPitch = Reading_GyroPitch - (StickPitch - GPS_Pitch);   // get difference
1223
    DiffPitch = Reading_GyroPitch - (StickPitch - GPS_Pitch);   // get difference
1218
    if(Gyro_I_Factor) SumPitch += IntegralPitch * Gyro_I_Factor - (StickPitch - GPS_Pitch); // I-part for attitude control
1224
    if(Gyro_I_Factor) SumPitch += IntegralPitch * Gyro_I_Factor - (StickPitch - GPS_Pitch); // I-part for attitude control
Line 1222... Line 1228...
1222
    pd_result = DiffPitch + Ki * SumPitch; // PI-controller for pitch
1228
    pd_result = DiffPitch + Ki * SumPitch; // PI-controller for pitch
Line 1223... Line 1229...
1223
 
1229
 
1224
    tmp_int = (int32_t)((int32_t)FCParam.DynamicStability * (int32_t)(ThrustMixFraction + abs(YawMixFraction)/2)) / 64;
1230
    tmp_int = (int32_t)((int32_t)FCParam.DynamicStability * (int32_t)(ThrustMixFraction + abs(YawMixFraction)/2)) / 64;
1225
    if(pd_result >  tmp_int) pd_result =  tmp_int;
1231
    if(pd_result >  tmp_int) pd_result =  tmp_int;
1226
    if(pd_result < -tmp_int) pd_result = -tmp_int;
1232
    if(pd_result < -tmp_int) pd_result = -tmp_int;
1227
 
-
 
1228
        // Motor Front
-
 
1229
    MotorValue = ThrustMixFraction + pd_result + YawMixFraction;          // Mixer
-
 
1230
        if ((MotorValue < 0)) MotorValue = 0;
-
 
1231
        else if(MotorValue > ParamSet.Trust_Max)            MotorValue = ParamSet.Trust_Max;
-
 
1232
        if (MotorValue < ParamSet.Trust_Min)            MotorValue = ParamSet.Trust_Min;
1233
       
1233
        Motor_Front = MotorValue;
1234
        PitchMixFraction = pd_result;  
1234
 
-
 
1235
 // Motor Rear
-
 
1236
        MotorValue = ThrustMixFraction - pd_result + YawMixFraction;     // Mixer
-
 
1237
        if ((MotorValue < 0)) MotorValue = 0;
-
 
1238
        else if(MotorValue > ParamSet.Trust_Max)            MotorValue = ParamSet.Trust_Max;
-
 
1239
        if (MotorValue < ParamSet.Trust_Min)            MotorValue = ParamSet.Trust_Min;
-
 
1240
        Motor_Rear = MotorValue;
1235
       
1241
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1236
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1242
// Roll-Axis
1237
// Roll-Axis
1243
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1238
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1244
        DiffRoll = Reading_GyroRoll - (StickRoll  - GPS_Roll);  // get difference
1239
        DiffRoll = Reading_GyroRoll - (StickRoll  - GPS_Roll);  // get difference
Line 1249... Line 1244...
1249
    pd_result = DiffRoll + Ki * SumRoll;         // PI-controller for roll
1244
    pd_result = DiffRoll + Ki * SumRoll;         // PI-controller for roll
1250
    tmp_int = (int32_t)((int32_t)FCParam.DynamicStability * (int32_t)(ThrustMixFraction + abs(YawMixFraction)/2)) / 64;
1245
    tmp_int = (int32_t)((int32_t)FCParam.DynamicStability * (int32_t)(ThrustMixFraction + abs(YawMixFraction)/2)) / 64;
1251
    if(pd_result >  tmp_int) pd_result =  tmp_int;
1246
    if(pd_result >  tmp_int) pd_result =  tmp_int;
1252
    if(pd_result < -tmp_int) pd_result = -tmp_int;
1247
    if(pd_result < -tmp_int) pd_result = -tmp_int;
Line -... Line 1248...
-
 
1248
 
-
 
1249
    RollMixFraction = pd_result;
-
 
1250
 
-
 
1251
// Calculate Motor Mixes
-
 
1252
       
-
 
1253
        // Motor FrontLeft
-
 
1254
    MotorValue =        ThrustMixFraction
-
 
1255
                                        + PitchMixFraction
-
 
1256
                                        + RollMixFraction/2
-
 
1257
                                        - YawMixFraction;         // Mixer
-
 
1258
        if ((MotorValue < 0)) MotorValue = 0;
-
 
1259
        else if(MotorValue > ParamSet.Trust_Max)            MotorValue = ParamSet.Trust_Max;
-
 
1260
        if (MotorValue < ParamSet.Trust_Min)            MotorValue = ParamSet.Trust_Min;
-
 
1261
        Motor_FrontLeft = MotorValue;
-
 
1262
 
-
 
1263
        // Motor FrontRight
-
 
1264
    MotorValue =        ThrustMixFraction
-
 
1265
                                        + PitchMixFraction
-
 
1266
                                        - RollMixFraction/2
-
 
1267
                                        + YawMixFraction;         // Mixer
-
 
1268
        if ((MotorValue < 0)) MotorValue = 0;
-
 
1269
        else if(MotorValue > ParamSet.Trust_Max)            MotorValue = ParamSet.Trust_Max;
-
 
1270
        if (MotorValue < ParamSet.Trust_Min)            MotorValue = ParamSet.Trust_Min;
-
 
1271
        Motor_FrontRight = MotorValue;
-
 
1272
 
-
 
1273
        // Motor RearLeft
-
 
1274
        MotorValue =    ThrustMixFraction
-
 
1275
                                        - PitchMixFraction
-
 
1276
                                        + RollMixFraction/2
-
 
1277
                                        - YawMixFraction;     // Mixer
-
 
1278
        if ((MotorValue < 0)) MotorValue = 0;
-
 
1279
        else if(MotorValue > ParamSet.Trust_Max)            MotorValue = ParamSet.Trust_Max;
-
 
1280
        if (MotorValue < ParamSet.Trust_Min)            MotorValue = ParamSet.Trust_Min;
-
 
1281
        Motor_RearLeft = MotorValue;
-
 
1282
        // Motor RearRight
-
 
1283
        MotorValue =    ThrustMixFraction
-
 
1284
                                        - PitchMixFraction
-
 
1285
                                        - RollMixFraction/2
-
 
1286
                                        + YawMixFraction;     // Mixer
-
 
1287
        if ((MotorValue < 0)) MotorValue = 0;
-
 
1288
        else if(MotorValue > ParamSet.Trust_Max)            MotorValue = ParamSet.Trust_Max;
-
 
1289
        if (MotorValue < ParamSet.Trust_Min)            MotorValue = ParamSet.Trust_Min;
-
 
1290
        Motor_RearRight= MotorValue;
1253
 
1291
       
1254
    // Motor Left
1292
    // Motor Left
-
 
1293
    MotorValue =        ThrustMixFraction
-
 
1294
                                        + RollMixFraction
1255
    MotorValue = ThrustMixFraction + pd_result - YawMixFraction;  // Mixer
1295
                                        + YawMixFraction;  // Mixer
1256
        if ((MotorValue < 0)) MotorValue = 0;
1296
        if ((MotorValue < 0)) MotorValue = 0;
1257
        else if(MotorValue > ParamSet.Trust_Max)                MotorValue = ParamSet.Trust_Max;
1297
        else if(MotorValue > ParamSet.Trust_Max)                MotorValue = ParamSet.Trust_Max;
1258
        if (MotorValue < ParamSet.Trust_Min)            MotorValue = ParamSet.Trust_Min;
1298
        if (MotorValue < ParamSet.Trust_Min)            MotorValue = ParamSet.Trust_Min;
Line 1259... Line 1299...
1259
    Motor_Left = MotorValue;
1299
    Motor_Left = MotorValue;
1260
 
1300
 
-
 
1301
        // Motor Right
-
 
1302
        MotorValue =    ThrustMixFraction
1261
 // Motor Right
1303
                                        - RollMixFraction
1262
        MotorValue = ThrustMixFraction - pd_result - YawMixFraction;  // Mixer
1304
                                        - YawMixFraction;  // Mixer
1263
        if ((MotorValue < 0)) MotorValue = 0;
1305
        if ((MotorValue < 0)) MotorValue = 0;
1264
        else if(MotorValue > ParamSet.Trust_Max)                MotorValue = ParamSet.Trust_Max;
1306
        else if(MotorValue > ParamSet.Trust_Max)                MotorValue = ParamSet.Trust_Max;
1265
        if (MotorValue < ParamSet.Trust_Min)            MotorValue = ParamSet.Trust_Min;
1307
        if (MotorValue < ParamSet.Trust_Min)            MotorValue = ParamSet.Trust_Min;