Advertisement
cyter

mk2code

Jul 31st, 2019
414
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 11.37 KB | None | 0 0
  1. //Includes
  2. #include "mbed.h"
  3. #include "rtos.h"
  4. #include "FreeIMU.h"
  5. #include "PID.h"
  6. #include "ConfigFile.h"
  7. #include "hardware.h"
  8. #include "statusLights.h"
  9. #include "serialPortMonitor.h"
  10. #include "flightController.h"
  11. #include "rcCommandMonitor.h"
  12. #include "altitudeMonitor.h"
  13.  
  14. //Declarations
  15. void LoadSettingsFromConfig();
  16. void InitialisePID();
  17. void InitialisePWM();
  18. void Setup();
  19.  
  20. //Loads settings from the config file - could tidy this a little if I can be assed
  21. void LoadSettingsFromConfig()
  22. {
  23.     _wiredSerial.printf("Starting to load settings from config file\n\r");
  24.    
  25.     //_wiredSerial.printf("Loading settings from config file\n\r");
  26.     char value[BUFSIZ];
  27.    
  28.     //Read a configuration file from a mbed.
  29.     if (!_configFile.read("/local/config.cfg"))
  30.     {
  31.         _wiredSerial.printf("Config file does not exist\n\r");
  32.     }
  33.     else
  34.     {    
  35.         //Get values
  36.         if (_configFile.getValue("_yawRatePIDControllerP", &value[0], sizeof(value))) _yawRatePIDControllerP = atof(value);
  37.         else
  38.         {
  39.             _wiredSerial.printf("Failed to get value for _yawRatePIDControllerP\n\r");
  40.         }
  41.         if (_configFile.getValue("_yawRatePIDControllerI", &value[0], sizeof(value))) _yawRatePIDControllerI = atof(value);
  42.             else
  43.         {
  44.             _wiredSerial.printf("Failed to get value for _yawRatePIDControllerI\n\r");
  45.         }
  46.         if (_configFile.getValue("_yawRatePIDControllerD", &value[0], sizeof(value))) _yawRatePIDControllerD = atof(value);
  47.             else
  48.         {
  49.             _wiredSerial.printf("Failed to get value for _yawRatePIDControllerD\n\r");
  50.         }
  51.         if (_configFile.getValue("_pitchRatePIDControllerP", &value[0], sizeof(value))) _pitchRatePIDControllerP = atof(value);
  52.             else
  53.         {
  54.             _wiredSerial.printf("Failed to get value for _pitchRatePIDControllerP\n\r");
  55.         }
  56.         if (_configFile.getValue("_pitchRatePIDControllerI", &value[0], sizeof(value))) _pitchRatePIDControllerI = atof(value);
  57.             else
  58.         {
  59.             _wiredSerial.printf("Failed to get value for _pitchRatePIDControllerI\n\r");
  60.         }
  61.         if (_configFile.getValue("_pitchRatePIDControllerD", &value[0], sizeof(value))) _pitchRatePIDControllerD = atof(value);
  62.             else
  63.         {
  64.             _wiredSerial.printf("Failed to get value for _pitchRatePIDControllerD\n\r");
  65.         }
  66.         if (_configFile.getValue("_rollRatePIDControllerP", &value[0], sizeof(value))) _rollRatePIDControllerP = atof(value);
  67.             else
  68.         {
  69.             _wiredSerial.printf("Failed to get value for _rollRatePIDControllerP\n\r");
  70.         }
  71.         if (_configFile.getValue("_rollRatePIDControllerI", &value[0], sizeof(value))) _rollRatePIDControllerI = atof(value);
  72.             else
  73.         {
  74.             _wiredSerial.printf("Failed to get value for _rollRatePIDControllerI\n\r");
  75.         }
  76.         if (_configFile.getValue("_rollRatePIDControllerD", &value[0], sizeof(value))) _rollRatePIDControllerD = atof(value);
  77.             else
  78.         {
  79.             _wiredSerial.printf("Failed to get value for _rollRatePIDControllerD\n\r");
  80.         }
  81.        
  82.         if (_configFile.getValue("_yawStabPIDControllerP", &value[0], sizeof(value))) _yawStabPIDControllerP = atof(value);
  83.             else
  84.         {
  85.             _wiredSerial.printf("Failed to get value for _yawStabPIDControllerP\n\r");
  86.         }
  87.         if (_configFile.getValue("_yawStabPIDControllerI", &value[0], sizeof(value))) _yawStabPIDControllerI = atof(value);
  88.             else
  89.         {
  90.             _wiredSerial.printf("Failed to get value for _yawStabPIDControllerI\n\r");
  91.         }
  92.         if (_configFile.getValue("_yawStabPIDControllerD", &value[0], sizeof(value))) _yawStabPIDControllerD = atof(value);
  93.             else
  94.         {
  95.             _wiredSerial.printf("Failed to get value for _yawStabPIDControllerD\n\r");
  96.         }
  97.         if (_configFile.getValue("_pitchStabPIDControllerP", &value[0], sizeof(value))) _pitchStabPIDControllerP = atof(value);
  98.             else
  99.         {
  100.             _wiredSerial.printf("Failed to get value for _pitchStabPIDControllerP\n\r");
  101.         }
  102.         if (_configFile.getValue("_pitchStabPIDControllerI", &value[0], sizeof(value))) _pitchStabPIDControllerI = atof(value);
  103.             else
  104.         {
  105.             _wiredSerial.printf("Failed to get value for _pitchStabPIDControllerI\n\r");
  106.         }
  107.         if (_configFile.getValue("_pitchStabPIDControllerD", &value[0], sizeof(value))) _pitchStabPIDControllerD = atof(value);
  108.             else
  109.         {
  110.             _wiredSerial.printf("Failed to get value for _pitchStabPIDControllerD\n\r");
  111.         }
  112.         if (_configFile.getValue("_rollStabPIDControllerP", &value[0], sizeof(value))) _rollStabPIDControllerP = atof(value);
  113.             else
  114.         {
  115.             _wiredSerial.printf("Failed to get value for _rollStabPIDControllerP\n\r");
  116.         }
  117.         if (_configFile.getValue("_rollStabPIDControllerI", &value[0], sizeof(value))) _rollStabPIDControllerI = atof(value);
  118.             else
  119.         {
  120.             _wiredSerial.printf("Failed to get value for _rollStabPIDControllerI\n\r");
  121.         }
  122.         if (_configFile.getValue("_rollStabPIDControllerD", &value[0], sizeof(value))) _rollStabPIDControllerD = atof(value);
  123.             else
  124.         {
  125.             _wiredSerial.printf("Failed to get value for _rollStabPIDControllerD\n\r");
  126.         }
  127.         if (_configFile.getValue("_zeroPitch", &value[0], sizeof(value))) _zeroValues[1] = atof(value);
  128.             else
  129.         {
  130.             _wiredSerial.printf("Failed to get value for zero pitch\n\r");
  131.         }
  132.         if (_configFile.getValue("_zeroRoll", &value[0], sizeof(value))) _zeroValues[2] = atof(value);
  133.             else
  134.         {
  135.             _wiredSerial.printf("Failed to get value for zero roll\n\r");
  136.         }
  137.     }
  138.    
  139.     _wiredSerial.printf("Finished loading settings from config file\n\r");
  140. }
  141.  
  142. //PID initialisation
  143. void InitialisePID()
  144. {
  145.     float updateTime = 1.0 / FLIGHT_CONTROLLER_FREQUENCY;
  146.    
  147.     _yawRatePIDController = new PID(_yawRatePIDControllerP, _yawRatePIDControllerI, _yawRatePIDControllerD, updateTime);
  148.     _yawRatePIDController->setInputLimits(IMU_YAW_RATE_MIN, IMU_YAW_RATE_MAX);
  149.     _yawRatePIDController->setOutputLimits(RATE_PID_CONTROLLER_OUTPUT_MIN, RATE_PID_CONTROLLER_OUTPUT_MAX);
  150.     _yawRatePIDController->setMode(AUTO_MODE);
  151.     _yawRatePIDController->setSetPoint(0.0);
  152.     _yawRatePIDController->setBias(0);
  153.    
  154.     _pitchRatePIDController = new PID(_pitchRatePIDControllerP, _pitchRatePIDControllerI, _pitchRatePIDControllerD, updateTime);
  155.     _pitchRatePIDController->setInputLimits(IMU_PITCH_RATE_MIN, IMU_PITCH_RATE_MAX);
  156.     _pitchRatePIDController->setOutputLimits(RATE_PID_CONTROLLER_OUTPUT_MIN, RATE_PID_CONTROLLER_OUTPUT_MAX);
  157.     _pitchRatePIDController->setMode(AUTO_MODE);
  158.     _pitchRatePIDController->setSetPoint(0.0);
  159.     _pitchRatePIDController->setBias(0);
  160.    
  161.     _rollRatePIDController = new PID(_rollRatePIDControllerP, _rollRatePIDControllerI, _rollRatePIDControllerD, updateTime);
  162.     _rollRatePIDController->setInputLimits(IMU_ROLL_RATE_MIN, IMU_ROLL_RATE_MAX);
  163.     _rollRatePIDController->setOutputLimits(RATE_PID_CONTROLLER_OUTPUT_MIN, RATE_PID_CONTROLLER_OUTPUT_MAX);
  164.     _rollRatePIDController->setMode(AUTO_MODE);
  165.     _rollRatePIDController->setSetPoint(0.0);
  166.     _rollRatePIDController->setBias(0);
  167.    
  168.     _yawStabPIDController = new PID(_yawStabPIDControllerP, _yawStabPIDControllerI, _yawStabPIDControllerD, updateTime);
  169.     _yawStabPIDController->setInputLimits(IMU_YAW_ANGLE_MIN, IMU_YAW_ANGLE_MAX);
  170.     _yawStabPIDController->setOutputLimits(IMU_YAW_RATE_MIN, IMU_YAW_RATE_MAX);
  171.     _yawStabPIDController->setMode(AUTO_MODE);
  172.     _yawStabPIDController->setSetPoint(0.0);
  173.     _yawStabPIDController->setBias(0);
  174.    
  175.     _pitchStabPIDController = new PID(_pitchStabPIDControllerP, _pitchStabPIDControllerI, _pitchStabPIDControllerD, updateTime);
  176.     _pitchStabPIDController->setInputLimits(IMU_PITCH_ANGLE_MIN, IMU_PITCH_ANGLE_MAX);
  177.     _pitchStabPIDController->setOutputLimits(IMU_PITCH_RATE_MIN, IMU_PITCH_RATE_MAX);
  178.     _pitchStabPIDController->setMode(AUTO_MODE);
  179.     _pitchStabPIDController->setSetPoint(0.0);
  180.     _pitchStabPIDController->setBias(0);
  181.    
  182.     _rollStabPIDController = new PID(_rollStabPIDControllerP, _rollStabPIDControllerI, _rollStabPIDControllerD, updateTime);
  183.     _rollStabPIDController->setInputLimits(IMU_ROLL_ANGLE_MIN, IMU_ROLL_ANGLE_MAX);
  184.     _rollStabPIDController->setOutputLimits(IMU_ROLL_RATE_MIN, IMU_ROLL_RATE_MAX);
  185.     _rollStabPIDController->setMode(AUTO_MODE);
  186.     _rollStabPIDController->setSetPoint(0.0);
  187.     _rollStabPIDController->setBias(0);
  188. }
  189.  
  190. //PWM Initialisation
  191. void InitialisePWM()
  192. {
  193.     //500Hz
  194.     float period = 1.0 / FLIGHT_CONTROLLER_FREQUENCY;
  195.     _motor1.period(period);
  196.     _motor2.period(period);
  197.     _motor3.period(period);
  198.     _motor4.period(period);
  199.    
  200.     //Disable
  201.     _motor1 = MOTORS_OFF;
  202.     _motor2 = MOTORS_OFF;
  203.     _motor2 = MOTORS_OFF;
  204.     _motor2 = MOTORS_OFF;
  205. }
  206.  
  207. //Setup
  208. void Setup()
  209. {
  210.     //Setup wired serial coms
  211.     _wiredSerial.baud(115200);
  212.    
  213.     printf("\r\n");  
  214.     printf("*********************************************************************************\r\n");
  215.     printf("Starting Setup\r\n");
  216.     printf("*********************************************************************************\r\n");
  217.    
  218.      //Disable buzzer
  219.     _buzzer = 0;
  220.    
  221.     //Setup wireless serial coms
  222.     _wirelessSerial.baud(57600);
  223.    
  224.     //Read config file
  225.     LoadSettingsFromConfig();
  226.    
  227.     //Set initial RC Ccommands
  228.     _rcMappedCommands[0] = 0;
  229.     _rcMappedCommands[1] = 0;
  230.     _rcMappedCommands[2] = 0;
  231.     _rcMappedCommands[3] = 0;
  232.    
  233.     //Setup RC median filters
  234.     _yawMedianFilter = new medianFilter(5);
  235.     _pitchMedianFilter = new medianFilter(5);
  236.     _rollMedianFilter = new medianFilter(5);
  237.     _thrustMedianFilter = new medianFilter(5);
  238.  
  239.     //Initialise PPM
  240.     _ppm = new PPM(_interruptPin, RC_OUT_MIN, RC_OUT_MAX, RC_IN_MIN, RC_IN_MAX, RC_CHANNELS, RC_THROTTLE_CHANNEL);
  241.  
  242.     //Initialise IMU
  243.     _freeIMU.init(true);
  244.    
  245.     //Initialise MaxBotix ping sensor
  246.     _maxBotixTimer.start();
  247.    
  248.     //Initialise GPS
  249.     _gps.baud(115200);
  250.    
  251.     //Initialise PID
  252.     InitialisePID();
  253.    
  254.     //Initialise PWM
  255.     InitialisePWM();
  256.    
  257.     //Set initialised flag
  258.     _initialised = true;
  259.        
  260.     // Start threads
  261.     _flightControllerThread = new Thread (FlightControllerThread);
  262.     _flightControllerThread->set_priority(osPriorityRealtime);
  263.     _rcCommandMonitorThread = new Thread (RcCommandMonitorThread);
  264.     _rcCommandMonitorThread->set_priority(osPriorityHigh);
  265.     _altitudeMonitorThread = new Thread (AltitudeMonitorThread);
  266.     _altitudeMonitorThread->set_priority(osPriorityHigh);
  267.     _serialPortMonitorThread = new Thread (SerialPortMonitorThread);
  268.     _serialPortMonitorThread->set_priority(osPriorityLow);
  269.     _statusThread = new Thread(StatusThread);
  270.     _statusThread->set_priority(osPriorityIdle);
  271.    
  272.     Thread::wait(1000);
  273.    
  274.     printf("*********************************************************************************\r\n");
  275.     printf("Finished Setup\r\n");
  276.     printf("*********************************************************************************\r\n");
  277. }
  278.  
  279. int main()
  280. {  
  281.     Setup();
  282.    
  283.     Thread::wait(osWaitForever);
  284. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement