dRonin  adbada4
dRonin firmware
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
storm32bgc.c
Go to the documentation of this file.
1 
14 /*
15  * This program is free software; you can redistribute it and/or modify
16  * it under the terms of the GNU General Public License as published by
17  * the Free Software Foundation; either version 3 of the License, or
18  * (at your option) any later version.
19  *
20  * This program is distributed in the hope that it will be useful, but
21  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
22  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
23  * for more details.
24  *
25  * You should have received a copy of the GNU General Public License along
26  * with this program; if not, see <http://www.gnu.org/licenses/>
27  *
28  * Additional note on redistribution: The copyright and license notices above
29  * must be maintained in each individual source file that is a derivative work
30  * of this source file; otherwise redistribution is prohibited.
31  */
32 
33 #include "openpilot.h"
34 #include "modulesettings.h"
35 #include "pios_thread.h"
36 #include "pios_queue.h"
37 #include "pios_mutex.h"
38 #include "uavobjectmanager.h"
39 #include "pios_modules.h"
40 
41 #include "cameradesired.h"
42 
43 // Private constants
44 #define STACK_SIZE_BYTES 512
45 #define TASK_PRIORITY PIOS_THREAD_PRIO_LOW
46 
47 #define STORM32BGC_PERIOD_MS 20 // 50 Hz
48 #define STORM32BGC_QUEUE_SIZE 64
49 
50 // Private types
51 
52 // Private variables
53 static struct pios_thread * storm32bgcTaskHandle;
54 static bool module_enabled;
56 static struct pios_recursive_mutex * mutex;
57 
58 // Private functions
59 static void storm32bgcTask(void *parameters);
60 
61 static inline uint16_t crc_calculate(uint8_t* pBuffer, int length);
62 
63 static uint8_t cmd_set_angle(float pitch, float roll, float yaw, uint8_t flags, uint8_t type);
64 
65 // Local variables
66 static uintptr_t storm32bgc_com_id;
67 
73 int32_t Storm32BgcInitialize(void)
74 {
76 
78 
79  if (!storm32bgc_com_id)
80  module_enabled = false;
81 
82  if (!module_enabled)
83  return -1;
84 
85  return 0;
86 }
87 
93 int32_t Storm32BgcStart(void)
94 {
95  //Check if module is enabled or not
96  if (module_enabled == false) {
97  return -1;
98  }
99 
100  // create storm32bgc queue
101  storm32bgc_queue = PIOS_Queue_Create(STORM32BGC_QUEUE_SIZE, sizeof(UAVObjEvent));
102  if (!storm32bgc_queue){
103  return -1;
104  }
105 
106  // Create mutex
107  mutex = PIOS_Recursive_Mutex_Create();
108  if (mutex == NULL){
109  return -2;
110  }
111 
112  // Start storm32bgc task
114 
115  TaskMonitorAdd(TASKINFO_RUNNING_STORM32BGC, storm32bgcTaskHandle);
116 
117  return 0;
118 }
119 
121 
122 static void storm32bgcTask(void *parameters)
123 {
124  uint32_t now = PIOS_Thread_Systime();
125 
126  CameraDesiredData cameraDesired;
127 
128  // Loop forever
129  while (1)
130  {
132 
133  CameraDesiredGet(&cameraDesired);
134 
135  float pitch_setpoint = cameraDesired.Declination;
136  float yaw_setpoint = cameraDesired.Bearing;
137 
138  switch(cmd_set_angle(pitch_setpoint, 0.0f, yaw_setpoint, 0x00, 0x00)) {
139 
140  case 0: // No response from Storm32Bgc
141  AlarmsSet(SYSTEMALARMS_ALARM_GIMBAL, SYSTEMALARMS_ALARM_WARNING);
142  break;
143 
144  case 1: // ACK response from Storm32Bgc
145  AlarmsSet(SYSTEMALARMS_ALARM_GIMBAL, SYSTEMALARMS_ALARM_OK);
146  break;
147 
148  default: // ACK response from Storm32Bgc invalid
149  AlarmsSet(SYSTEMALARMS_ALARM_GIMBAL, SYSTEMALARMS_ALARM_CRITICAL);
150  }
151  }
152 }
153 
154 union {
155  uint16_t value;
156  uint8_t bytes[2];
157 } crc;
158 
159 union {
160  uint16_t value;
161  uint8_t bytes[2];
162 } dataU16;
163 
164 union {
165  float value;
166  uint8_t bytes[4];
167 } dataFloat;
168 
175 #define X25_INIT_CRC 0xFFFF
176 
186 static inline void crc_accumulate(uint8_t data, uint16_t *crcAccum)
187 {
188  /*Accumulate one byte of data into the CRC*/
189  uint8_t tmp;
190 
191  tmp = data ^ (uint8_t)(*crcAccum &0xFF);
192  tmp ^= (tmp<<4);
193  *crcAccum = (*crcAccum>>8) ^ (tmp<<8) ^ (tmp <<3) ^ (tmp>>4);
194 }
195 
203 static inline uint16_t crc_calculate(uint8_t* pBuffer, int length)
204 {
205  // For a "message" of length bytes contained in the unsigned char array
206  // pointed to by pBuffer, calculate the CRC
207  // crcCalculate(unsigned char* pBuffer, int length, unsigned short* checkConst) < not needed
208 
209  uint16_t crcTmp;
210  uint8_t* pTmp;
211  int i;
212 
213  pTmp=pBuffer;
214 
215  /* init crcTmp */
216  crcTmp = X25_INIT_CRC;
217 
218  for (i = 0; i < length; i++){
219  crc_accumulate(*pTmp++, &crcTmp);
220  }
221 
222  return(crcTmp);
223 }
224 
237 static uint8_t cmd_set_angle(float pitch, float roll, float yaw, uint8_t flags, uint8_t type)
238 {
239  uint8_t command_string[19];
240  uint8_t read_data[6] = {0,};
241  uint8_t bytes_read;
242  uint8_t ack;
243 
244  command_string[0] = 0xFA; // Start Sign
245  command_string[1] = 0x0E; // Length of Payload
246  command_string[2] = 0x11; // Command Byte - CMD_SETANGLE
247 
248  dataFloat.value = pitch;
249  command_string[3] = dataFloat.bytes[0]; // pitch-byte0
250  command_string[4] = dataFloat.bytes[1]; // pitch-byte1
251  command_string[5] = dataFloat.bytes[2]; // pitch-byte2
252  command_string[6] = dataFloat.bytes[3]; // pitch-byte3
253 
254  dataFloat.value = roll;
255  command_string[7] = dataFloat.bytes[0]; // roll-byte0
256  command_string[8] = dataFloat.bytes[1]; // roll-byte1
257  command_string[9] = dataFloat.bytes[2]; // roll-byte2
258  command_string[10] = dataFloat.bytes[3]; // roll-byte3
259 
260  dataFloat.value = yaw;
261  command_string[11] = dataFloat.bytes[0]; // yaw-byte0
262  command_string[12] = dataFloat.bytes[1]; // yaw-byte1
263  command_string[13] = dataFloat.bytes[2]; // yaw-byte2
264  command_string[14] = dataFloat.bytes[3]; // yaw-byte3
265 
266  command_string[15] = flags; // flags-byte
267 
268  command_string[16] = type; // type-byte
269 
270  crc.value = crc_calculate(&command_string[1], 16); // Does not include Start Sign
271 
272  command_string[17] = crc.bytes[0];
273  command_string[18] = crc.bytes[1];
274 
275  PIOS_COM_SendBuffer(storm32bgc_com_id, &command_string[0], 19);
276 
277  bytes_read = PIOS_COM_ReceiveBuffer(storm32bgc_com_id, read_data, 6, 500);
278 
279  if (bytes_read != 0)
280  {
281  crc.value = crc_calculate(&read_data[1], 3); // Does not include Start Sign
282 
283  if ((crc.bytes[0] == read_data[4]) && (crc.bytes[1] == read_data[5]))
284  {
285  ack = read_data[3];
286 
287  ack++;
288  }
289  else
290  ack = 200;
291  }
292  else
293  ack = 0;
294 
295  return ack;
296 }
297 
union @12 crc
uint32_t PIOS_Thread_Systime(void)
Definition: pios_thread.c:212
#define STORM32BGC_PERIOD_MS
Definition: storm32bgc.c:47
int32_t Storm32BgcInitialize(void)
Definition: storm32bgc.c:73
struct pios_queue * PIOS_Queue_Create(size_t queue_length, size_t item_size)
Definition: pios_queue.c:47
#define X25_INIT_CRC
Definition: storm32bgc.c:175
struct _msp_pid_item pitch
Definition: msp_messages.h:97
uint16_t value
Definition: storm32bgc.c:160
bool PIOS_Modules_IsEnabled(enum pios_modules module)
Definition: pios_modules.c:41
#define TASK_PRIORITY
Definition: storm32bgc.c:45
struct _msp_pid_item roll
Definition: msp_messages.h:96
static uint16_t crc_calculate(uint8_t *pBuffer, int length)
Calculates the X.25 checksum on a byte buffer.
Definition: storm32bgc.c:203
int32_t AlarmsSet(SystemAlarmsAlarmElem alarm, SystemAlarmsAlarmOptions severity)
Definition: alarms.c:97
static struct pios_thread * storm32bgcTaskHandle
Definition: storm32bgc.c:53
union @13 dataU16
uint16_t flags
Definition: uavtalk_priv.h:52
uint8_t bytes[2]
Definition: storm32bgc.c:156
uint8_t data[XFER_BYTES_PER_PACKET]
Definition: bl_messages.h:129
uint8_t length
struct pios_recursive_mutex * PIOS_Recursive_Mutex_Create(void)
Definition: pios_mutex.c:115
static uintptr_t storm32bgc_com_id
Definition: storm32bgc.c:66
#define PIOS_COM_STORM32BGC
Definition: pios_board.h:123
static uint8_t cmd_set_angle(float pitch, float roll, float yaw, uint8_t flags, uint8_t type)
Definition: storm32bgc.c:237
uint16_t PIOS_COM_ReceiveBuffer(uintptr_t com_id, uint8_t *buf, uint16_t buf_len, uint32_t timeout_ms)
static void crc_accumulate(uint8_t data, uint16_t *crcAccum)
Accumulate the X.25 CRC by adding one char at a time.
Definition: storm32bgc.c:186
struct pios_thread * PIOS_Thread_Create(void(*fp)(void *), const char *namep, size_t stack_bytes, void *argp, enum pios_thread_prio_e prio)
Definition: pios_thread.c:89
static void storm32bgcTask(void *parameters)
Definition: storm32bgc.c:122
int32_t TaskMonitorAdd(TaskInfoRunningElem task, struct pios_thread *threadp)
Definition: taskmonitor.c:67
uint8_t i
Definition: msp_messages.h:97
struct _msp_pid_item yaw
Definition: msp_messages.h:98
uint16_t value
Definition: storm32bgc.c:155
void PIOS_Thread_Sleep_Until(uint32_t *previous_ms, uint32_t increment_ms)
Definition: pios_thread.c:255
PIOS_COM_SendBuffer(shub_global->frsky_port, shub_global->serial_buf, msg_length)
#define STORM32BGC_QUEUE_SIZE
Definition: storm32bgc.c:48
uint8_t type
union @14 dataFloat
tuple f
Definition: px_mkfw.py:81
static struct pios_recursive_mutex * mutex
Definition: storm32bgc.c:56
Includes PiOS and core architecture components.
static bool module_enabled
Definition: storm32bgc.c:54
float value
Definition: storm32bgc.c:165
int32_t Storm32BgcStart(void)
Definition: storm32bgc.c:93
#define STACK_SIZE_BYTES
Definition: storm32bgc.c:44
MODULE_INITCALL(Storm32BgcInitialize, Storm32BgcStart)
struct pios_queue * storm32bgc_queue
Definition: storm32bgc.c:55