Advertisement
Guest User

minimal_quadrotor

a guest
May 20th, 2018
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.14 KB | None | 0 0
  1. #include "your_code.h"
  2. #include <stdio.h>
  3.  
  4.  
  5. // Minimal controller, just turns on the motors.
  6.  
  7. /***
  8.  *
  9.  * This file is where you should add you tasks. You already know the structure
  10.  * Required to do so from the work with the simulator.
  11.  *
  12.  * The function yourCodeInit() is set to automatically execute when the
  13.  * quadrotor is started. This is where you need to create your tasks. The
  14.  * scheduler that runs the tasks is already up and running so you should
  15.  * NOT make a call to vTaskStartScheduler();.
  16.  *
  17.  * Below that you can find a few examples of useful function calls and code snippets.
  18.  *
  19.  * For further reference on how this is done. Look into the file stabilizer.c
  20.  * which is usually handles the control of the crazyflie.
  21.  *
  22.  ***/
  23.  
  24. #define D2R 3.14159265359f/180.0f
  25. #define R2D 180.0f/3.14159265359f
  26.  
  27. #define SAMPLEPERIOD_MS 4
  28.  
  29. xTaskHandle complementaryFilterTaskHandle;
  30. xTaskHandle setPointValueGeneratorHandle;
  31. xTaskHandle LQControllerHandle;
  32.  
  33. void complementaryFilterTask() {
  34.    
  35.     portTickType xLastWakeTime;
  36.    
  37.     // Wait for sensor to be calibrated
  38.     xLastWakeTime = xTaskGetTickCount();
  39.     while(!sensorsAreCalibrated()) {
  40.         vTaskDelayUntil(&xLastWakeTime, F2T(RATE_MAIN_LOOP));
  41.     }
  42.  
  43.     xLastWakeTime = xTaskGetTickCount();
  44.     for (;;) {
  45.        
  46.         // TODO Get sensor data and do filter stuff....
  47.         // Get sensor data.
  48.        
  49.         vTaskDelayUntil(&xLastWakeTime, (SAMPLEPERIOD_MS / portTICK_RATE_MS) );
  50.     }
  51.    
  52.     vTaskDelete(complementaryFilterTaskHandle);
  53. }
  54.  
  55. void setPointValueGeneratorTask(void *pvParameters) {
  56.     portTickType xLastWakeTime = xTaskGetTickCount();
  57.  
  58.     for (;;) {
  59.         // TODO Get setpoint stuff
  60.  
  61.         vTaskDelayUntil(&xLastWakeTime, (SAMPLEPERIOD_MS / portTICK_RATE_MS) );
  62.     }
  63.    
  64.     vTaskDelete(setPointValueGeneratorHandle);
  65. }
  66.  
  67. void LQControllerTask(void *pvParameters) {
  68.    
  69.     portTickType xLastWakeTime = xTaskGetTickCount();
  70.  
  71.     for (;;) {
  72.  
  73.         uint16_t value_1 = 5000;
  74.  
  75.         motorsSetRatio(MOTOR_M1, value_1);
  76.         motorsSetRatio(MOTOR_M2, value_1);
  77.         motorsSetRatio(MOTOR_M3, value_1);
  78.         motorsSetRatio(MOTOR_M4, value_1);
  79.  
  80.         vTaskDelayUntil(&xLastWakeTime, (SAMPLEPERIOD_MS / portTICK_RATE_MS) );
  81.     }
  82.    
  83.     vTaskDelete(LQControllerHandle);
  84. }
  85.  
  86.  
  87. void yourCodeInit(void)
  88. {
  89.     xTaskCreate(complementaryFilterTask, "Gather sensor data", configMINIMAL_STACK_SIZE, NULL, 5, &complementaryFilterTaskHandle);
  90.     xTaskCreate(setPointValueGeneratorTask, "Set point value generator", configMINIMAL_STACK_SIZE, NULL, 3, &setPointValueGeneratorHandle);
  91.     xTaskCreate(LQControllerTask, "Controller task", configMINIMAL_STACK_SIZE, NULL, 4, &LQControllerHandle);
  92.     motorsSetRatio(MOTOR_M1, 10000);
  93.  
  94.     // Scheduler is started elsewhere.
  95. }
  96.  
  97.  
  98.  
  99. /*************************************************
  100.  * WAIT FOR SENSORS TO BE CALIBRATED
  101.  ************************************************/
  102. // lastWakeTime = xTaskGetTickCount ();
  103. // while(!sensorsAreCalibrated()) {
  104. //     vTaskDelayUntil(&lastWakeTime, F2T(RATE_MAIN_LOOP));
  105. // }
  106.  
  107.  
  108.  
  109. /*************************************************
  110.  * RETRIEVE THE MOST RECENT SENSOR DATA
  111.  *
  112.  * The code creates a variable called sensorData and then calls a function
  113.  * that fills this variable with the latest data from the sensors.
  114.  *
  115.  * sensorData_t sensorData = struct {
  116.  *     Axis3f acc;
  117.  *     Axis3f gyro;
  118.  *     Axis3f mag;
  119.  *     baro_t baro;
  120.  *     zDistance_t zrange;
  121.  *     point_t position;
  122.  * }
  123.  *
  124.  ************************************************/
  125. // sensorData_t sensorData;
  126. // sensorsAcquire(&sensorData);
  127.  
  128.  
  129.  
  130. /*************************************************
  131.  * RETRIEVE THE SET POINT FROM ANY EXTERNAL COMMAND INTERFACE
  132.  *
  133.  * The code creates a variable called setpoint and then calls a function
  134.  * that fills this variable with the latest command input.
  135.  *
  136.  * setpoint_t setpoint = struct {
  137.  *     uint32_t timestamp;
  138.  *
  139.  *     attitude_t attitude;      // deg
  140.  *     attitude_t attitudeRate;  // deg/s
  141.  *     quaternion_t attitudeQuaternion;
  142.  *     float thrust;
  143.  *     point_t position;         // m
  144.  *     velocity_t velocity;      // m/s
  145.  *     acc_t acceleration;       // m/s^2
  146.  *     bool velocity_body;       // true if velocity is given in body frame; false if velocity is given in world frame
  147.  *
  148.  *     struct {
  149.  *         stab_mode_t x;
  150.  *         stab_mode_t y;
  151.  *         stab_mode_t z;
  152.  *         stab_mode_t roll;
  153.  *         stab_mode_t pitch;
  154.  *         stab_mode_t yaw;
  155.  *         stab_mode_t quat;
  156.  *     } mode;
  157.  * }
  158.  *
  159.  ************************************************/
  160. // setpoint_t setpoint;
  161. // commanderGetSetpoint(&setpoint);
  162.  
  163.  
  164.  
  165. /*************************************************
  166.  * SENDING OUTPUT TO THE MOTORS
  167.  *
  168.  * The code sends an output to each motor. The output should have the be
  169.  * of the typ unsigned 16-bit integer, i.e. use variables such as:
  170.  * uint16_t value_i
  171.  *
  172.  ************************************************/
  173. // motorsSetRatio(MOTOR_M1, value_1);
  174. // motorsSetRatio(MOTOR_M2, value_2);
  175. // motorsSetRatio(MOTOR_M3, value_3);
  176. // motorsSetRatio(MOTOR_M4, value_4);
  177.  
  178.  
  179. /*************************************************
  180.  * LOGGING VALUES THAT CAN BE PLOTTEN IN PYTHON CLIENT
  181.  *
  182.  * We have already set up three log blocks to for the accelerometer data, the
  183.  * gyro data and the setpoints, just uncomment the block to start logging. Use
  184.  * them as reference if you want to add custom blocks.
  185.  *
  186.  ************************************************/
  187.  
  188.  
  189. //LOG_GROUP_START(acc)
  190. //LOG_ADD(LOG_FLOAT, x, &sensorData.acc.x)
  191. //LOG_ADD(LOG_FLOAT, y, &sensorData.acc.y)
  192. //LOG_ADD(LOG_FLOAT, z, &sensorData.acc.z)
  193. //LOG_GROUP_STOP(acc)
  194.  
  195.  
  196.  
  197. //LOG_GROUP_START(gyro)
  198. //LOG_ADD(LOG_FLOAT, x, &sensorData.gyro.x)
  199. //LOG_ADD(LOG_FLOAT, y, &sensorData.gyro.y)
  200. //LOG_ADD(LOG_FLOAT, z, &sensorData.gyro.z)
  201. //LOG_GROUP_STOP(gyro)
  202.  
  203.  
  204.  
  205. //LOG_GROUP_START(ctrltarget)
  206. //LOG_ADD(LOG_FLOAT, roll, &setpoint.attitude.roll)
  207. //LOG_ADD(LOG_FLOAT, pitch, &setpoint.attitude.pitch)
  208. //LOG_ADD(LOG_FLOAT, yaw, &setpoint.attitudeRate.yaw)
  209. //LOG_ADD(LOG_FLOAT, base, &setpoint.thrust)
  210. //LOG_GROUP_STOP(ctrltarget)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement