Advertisement
Guest User

commander.c

a guest
Jun 20th, 2014
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.36 KB | None | 0 0
  1. /**
  2.  *    ||          ____  _ __
  3.  * +------+      / __ )(_) /_______________ _____  ___
  4.  * | 0xBC |     / __  / / __/ ___/ ___/ __ `/_  / / _ \
  5.  * +------+    / /_/ / / /_/ /__/ /  / /_/ / / /_/  __/
  6.  *  ||  ||    /_____/_/\__/\___/_/   \__,_/ /___/\___/
  7.  *
  8.  * Crazyflie Firmware
  9.  *
  10.  * Copyright (C) 2011-2012 Bitcraze AB
  11.  *
  12.  * This program is free software: you can redistribute it and/or modify
  13.  * it under the terms of the GNU General Public License as published by
  14.  * the Free Software Foundation, in version 3.
  15.  *
  16.  * This program is distributed in the hope that it will be useful,
  17.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19.  * GNU General Public License for more details.
  20.  *
  21.  * You should have received a copy of the GNU General Public License
  22.  * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23.  *
  24.  *
  25.  */
  26. #include "stm32f10x_conf.h"
  27.  
  28. #include "FreeRTOS.h"
  29. #include "task.h"
  30.  
  31. #include "commander.h"
  32. #include "crtp.h"
  33. #include "configblock.h"
  34. #include "param.h"
  35.  
  36. #define MIN_THRUST  10000
  37. #define MAX_THRUST  60000
  38.  
  39. struct CommanderCrtpValues
  40. {
  41.   float roll;
  42.   float pitch;
  43.   float yaw;
  44.   uint16_t thrust;
  45.   uint8_t r;
  46.   uint8_t g;
  47.   uint8_t b;
  48. } __attribute__((packed));
  49.  
  50. static struct CommanderCrtpValues targetVal[2];
  51. static bool isInit;
  52. static int side=0;
  53. static uint32_t lastUpdate;
  54. static bool isInactive;
  55. static bool altHoldMode = FALSE;
  56. static bool altHoldModeOld = FALSE;
  57.  
  58. static void commanderCrtpCB(CRTPPacket* pk);
  59. static void commanderWatchdogReset(void);
  60.  
  61. void commanderInit(void)
  62. {
  63.   if(isInit)
  64.     return;
  65.  
  66.  
  67.   crtpInit();
  68.   crtpRegisterPortCB(CRTP_PORT_COMMANDER, commanderCrtpCB);
  69.  
  70.   lastUpdate = xTaskGetTickCount();
  71.   isInactive = TRUE;
  72.   isInit = TRUE;
  73. }
  74.  
  75. bool commanderTest(void)
  76. {
  77.   crtpTest();
  78.   return isInit;
  79. }
  80.  
  81. static void commanderCrtpCB(CRTPPacket* pk)
  82. {
  83.   targetVal[!side] = *((struct CommanderCrtpValues*)pk->data);
  84.   side = !side;
  85.   commanderWatchdogReset();
  86. }
  87.  
  88. void commanderWatchdog(void)
  89. {
  90.   int usedSide = side;
  91.   uint32_t ticktimeSinceUpdate;
  92.  
  93.   ticktimeSinceUpdate = xTaskGetTickCount() - lastUpdate;
  94.  
  95.   if (ticktimeSinceUpdate > COMMANDER_WDT_TIMEOUT_STABALIZE)
  96.   {
  97.     targetVal[usedSide].roll = 0;
  98.     targetVal[usedSide].pitch = 0;
  99.     targetVal[usedSide].yaw = 0;
  100.   }
  101.   if (ticktimeSinceUpdate > COMMANDER_WDT_TIMEOUT_SHUTDOWN)
  102.   {
  103.     targetVal[usedSide].r = 0;
  104.     targetVal[usedSide].g = 0;
  105.     targetVal[usedSide].b = 0;
  106.     targetVal[usedSide].thrust = 0;
  107.     altHoldMode = FALSE; // do we need this? It would reset the target altitude upon reconnect if still hovering
  108.     isInactive = TRUE;
  109.   }
  110.   else
  111.   {
  112.     isInactive = FALSE;
  113.   }
  114. }
  115.  
  116. static void commanderWatchdogReset(void)
  117. {
  118.   lastUpdate = xTaskGetTickCount();
  119. }
  120.  
  121. uint32_t commanderGetInactivityTime(void)
  122. {
  123.   return xTaskGetTickCount() - lastUpdate;
  124. }
  125.  
  126. void commanderGetRPY(float* eulerRollDesired, float* eulerPitchDesired, float* eulerYawDesired)
  127. {
  128.   int usedSide = side;
  129.  
  130.   *eulerRollDesired  = targetVal[usedSide].roll;
  131.   *eulerPitchDesired = targetVal[usedSide].pitch;
  132.   *eulerYawDesired   = targetVal[usedSide].yaw;
  133. }
  134.  
  135. void commanderGetAltHold(bool* altHold, bool* setAltHold, float* altHoldChange)
  136. {
  137.   *altHold = altHoldMode; // Still in altitude hold mode
  138.   *setAltHold = !altHoldModeOld && altHoldMode; // Hover just activated
  139.   *altHoldChange = altHoldMode ? ((float) targetVal[side].thrust - 32767.) / 32767. : 0.0; // Amount to change altitude hold target
  140.   altHoldModeOld = altHoldMode;
  141. }
  142.  
  143.  
  144. void commanderGetRPYType(RPYType* rollType, RPYType* pitchType, RPYType* yawType)
  145. {
  146.   *rollType  = ANGLE;
  147.   *pitchType = ANGLE;
  148.   *yawType   = RATE;
  149. }
  150.  
  151. void commanderGetThrust(uint16_t* thrust)
  152. {
  153.   int usedSide = side;
  154.   uint16_t rawThrust = targetVal[usedSide].thrust;
  155.  
  156.   if (rawThrust > MIN_THRUST)
  157.   {
  158.     *thrust = rawThrust;
  159.   }
  160.   else
  161.   {
  162.     *thrust = 0;
  163.   }
  164.  
  165.   if (rawThrust > MAX_THRUST)
  166.   {
  167.     *thrust = MAX_THRUST;
  168.   }
  169.  
  170.   commanderWatchdog();
  171. }
  172.  
  173. void commanderGetRGB(uint8_t* r, uint8_t* g, uint8_t* b)
  174. {
  175.   int usedSide = side;
  176.  
  177.   *r  = targetVal[usedSide].r;
  178.   *g = targetVal[usedSide].g;
  179.   *b   = targetVal[usedSide].b;
  180. }
  181.  
  182. // Params for flight modes
  183. PARAM_GROUP_START(flightmode)
  184. PARAM_ADD(PARAM_UINT8, althold, &altHoldMode)
  185. PARAM_GROUP_STOP(flightmode)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement