Debian Codegraph @

thirdparty/analogdevicesinc/no-OS/device_commands/CN0189/Command.c

raw symbols SCIP
1 /**************************************************************************//**
2 * @file Commanad.c
3 * @brief Implementation of the commands given by user through UART for CN0189.
4 * @author Lucian Sin ([email protected])
5 *
6 ********************************************************************************
7 * Copyright 2013(c) Analog Devices, Inc.
8 *
9 * All rights reserved.
10 *
11 * Redistribution and use in source and binary forms, with or without modification,
12 * are permitted provided that the following conditions are met:
13 * - Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * - Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
18 * distribution.
19 * - Neither the name of Analog Devices, Inc. nor the names of its
20 * contributors may be used to endorse or promote products derived
21 * from this software without specific prior written permission.
22 * - The use of this software may or may not infringe the patent rights
23 * of one or more patent holders. This license does not release you
24 * from the requirement that you obtain separate licenses from these
25 * patent holders to use this software.
26 * - Use of the software either in source or binary form, must be run
27 * on or directly connected to an Analog Devices Inc. component.
28 *
29 * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR IMPLIED
30 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT, MERCHANTABILITY
31 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
32 * IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
34 * INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
35 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
36 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
38 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 *
40 *******************************************************************************/
41
42 /******************************************************************************/
43 /***************************** Include Files **********************************/
44 /******************************************************************************/
45 #include "Command.h"
46 #include "Console.h"
47 #include "ad7887.h"
48 #include "TIME.h"
49 #include <math.h>
50
51 /******************************************************************************/
52 /************************ Constants Definitions *******************************/
53 /******************************************************************************/
54
55 const struct cmd_info cmdList[] = {
56 [0] = {
57 .name = "help?",
58 .description = "Displays all available commands.",
59 .acceptedValue = "",
60 .example = "",
61 },
62 [1] = {
63 .name = "calibX+1G!",
64 .description = "Acquire data for calibration, when Xacc=+1[g].",
65 .acceptedValue = "",
66 .example = "",
67 },
68 [2] = {
69 .name = "calibX-1G!",
70 .description = "Acquire data for calibration, when Xacc=-1[g].",
71 .acceptedValue = "",
72 .example = "",
73 },
74 [3] = {
75 .name = "calibY+1G!",
76 .description = "Acquire data for calibration, when Yacc=+1[g].",
77 .acceptedValue = "",
78 .example = "",
79 },
80 [4] = {
81 .name = "calibY-1G!",
82 .description = "Acquire data for calibration, when Yacc=-1[g].",
83 .acceptedValue = "",
84 .example = "",
85 },
86 [5] = {
87 .name = "calibCalculateParam!",
88 .description = "Calculates the parameters for calibration.",
89 .acceptedValue = "",
90 .example = "",
91 },
92 [6] = {
93 .name = "rawData?",
94 .description = "Displays the raw data an both axes.(40 samples)",
95 .acceptedValue = "",
96 .example = "",
97 },
98 [7] = {
99 .name = "acceleration?",
100 .description = "Displays the acceleration on both axes.(40 samples)",
101 .acceptedValue = "",
102 .example = "",
103 },
104 [8] = {
105 .name = "tilt?",
106 .description = "Displays the tilt on both axes.(40 samples)",
107 .acceptedValue = "",
108 .example = "",
109 },
110 };
111
112 const char cmdNo = (sizeof(cmdList) / sizeof(struct cmd_info));
113
114 /******************************************************************************/
115 /************************ Variables Definitions *******************************/
116 /******************************************************************************/
117 cmdFunction cmdFunctions[] = {GetHelp,GetCalibrationXplus1G,
118 GetCalibrationXminus1G,GetCalibrationYplus1G,
119 GetCalibrationYminus1G,DoCalibrationCalculateParam,
120 GetRawData,GetAcceleration,GetTilt};
121
122 /* Variables holding information about the device */
123 unsigned char dataX[2] = {0x00, 0x00};
124 unsigned char dataY[2] = {0x00, 0x00};
125 float offsetX = 72.5f;
126 float gainX = 1.253f;
127 float offsetY = 57.5f;
128 float gainY = 1.2789f;
129 float samples[4][2] = {{0,0},{0,0},{0,0},{0,0}}; //calibration readings
130
131 /***************************************************************************//**
132 * @brief Displays all available commands.
133 *
134 * @return None.
135 *******************************************************************************/
136 void GetHelp(double* param, char paramNo) // "help?" command
137 {
138 unsigned char displayCmd;
139
140 CONSOLE_Print("Available commands:\r\n");
141 for(displayCmd = 0; displayCmd < cmdNo; displayCmd++)
142 {
143 CONSOLE_Print("%s - %s %s\r\n", (char*)cmdList[displayCmd].name,
144 (char*)cmdList[displayCmd].description,
145 (char*)cmdList[displayCmd].acceptedValue);
146 }
147 }
148
149 /***************************************************************************//**
150 * @brief Internal function for displaying all the command with its description.
151 *
152 * @return None.
153 *******************************************************************************/
154 void DisplayCmdList()
155 {
156 unsigned char displayCmd;
157
158 for(displayCmd = 0; displayCmd < cmdNo; displayCmd++)
159 {
160 CONSOLE_Print("%s - %s\r\n", (char*)cmdList[displayCmd].name, \
161 (char*)cmdList[displayCmd].description);
162 }
163 }
164
165 /***************************************************************************//**
166 * @brief Displays error message.
167 *
168 * @return None.
169 *******************************************************************************/
170 void DisplayError(unsigned char funcNo)
171 {
172 /* Display error messages */
173 CONSOLE_Print("Invalid parameter!\r\n");
174 CONSOLE_Print("%s - %s %s\r\n", (char*)cmdList[funcNo].name, \
175 (char*)cmdList[funcNo].description, \
176 (char*)cmdList[funcNo].acceptedValue);
177 CONSOLE_Print("Example: %s\r\n", (char*)cmdList[funcNo].example);
178 }
179
180 /***************************************************************************//**
181 * @brief Initializes the device.
182 *
183 * @return - The result of the initialization.
184 * Example: ERROR - the device was not initialized or the device
185 * is not present.
186 * SUCCES - the device was initialized and the device
187 * is present.
188 *******************************************************************************/
189 char DoDeviceInit(void)
190 {
191 if(AD7887_Init() == 0)
192 {
193 CONSOLE_Print("Device OK\r\n");
194 /* Give a value to control words for X and Y axes for AD7887_Read16Bits()
195 function. */
196 dataX[0] = ad7887_CTRL_REF | ad7887_CTRL_SIN_DUAL | ad7887_CTRL_PM0;
197 dataY[0] = ad7887_CTRL_REF | ad7887_CTRL_SIN_DUAL | ad7887_CTRL_CH |
198 ad7887_CTRL_PM0;
199 DisplayCmdList();
200 return SUCCESS;
201 }
202 else
203 {
204 CONSOLE_Print("Device Error\r\n");
205 return ERROR;
206 }
207 }
208
209 /***************************************************************************//**
210 * @brief Displays the raw data on both axes.(40 samples)
211 *
212 * @return None.
213 *******************************************************************************/
214 void GetRawData(double* param, char paramNo) // "rawData?" command
215 {
216 unsigned short ch0RawData = 0;
217 unsigned short ch1RawData = 0;
218 signed short rawDataX = 0;
219 signed short rawDataY = 0;
220 unsigned char count = 0;
221
222 for (count = 0; count < 40; count++)
223 {
224 ch0RawData = (AD7887_Read16Bits(dataX)) & 0x0FFF;
225 ch1RawData = (AD7887_Read16Bits(dataY)) & 0x0FFF;
226 /* Be able to have also negative values. */
227 rawDataX = (signed short)ch0RawData - 2048;
228 rawDataY = (signed short)ch1RawData - 2048;
229 /* Send feedback to user */
230 CONSOLE_Print("Raw Data X axis=%d ",rawDataX);
231 CONSOLE_Print("Raw Data Y axis=%d\r\n",rawDataY);
232 /* Time delay in order to be able to see the changes in real time */
233 TIME_DelayMs(200);
234 }
235 }
236
237 /***************************************************************************//**
238 * @brief Displays the acceleration on both axes.(40 samples)
239 *
240 * @return None.
241 *******************************************************************************/
242 void GetAcceleration(double* param, char paramNo) // "acceleration?" command
243 {
244 unsigned short ch0RawData = 0;
245 unsigned short ch1RawData = 0;
246 float rawDataX = 0;
247 float rawDataY = 0;
248 float ch0CorrectedData = 0;
249 float ch1CorrectedData = 0;
250 float accelX = 0;
251 float accelY = 0;
252 unsigned char count = 0;
253
254 for (count = 0; count < 40; count++)
255 {
256 ch0RawData = (AD7887_Read16Bits(dataX)) & 0x0FFF;
257 ch1RawData = (AD7887_Read16Bits(dataY)) & 0x0FFF;
258 /* Be able to have also negative values. */
259 rawDataX = (float)ch0RawData - 2048;
260 rawDataY = (float)ch1RawData - 2048;
261 /* Compute the corrected data using offset and gain. */
262 ch0CorrectedData = (rawDataX - offsetX) * gainX;
263 ch1CorrectedData = (rawDataY - offsetY) * gainY;
264 /* Compute the acceleration values on the X and Y axis. */
265 accelX = ((float)ch0CorrectedData) / ((1 << 11));
266 accelY = ((float)ch1CorrectedData) / ((1 << 11));
267 /* Limit the X acceleration to 1g. */
268 if(fabs(accelX) > 1.0f)
269 {
270 accelX = accelX / fabs(accelX);
271 }
272 /* Limit the Y acceleration to 1g. */
273 if(fabs(accelY) > 1.0f)
274 {
275 accelY = accelY / fabs(accelY);
276 }
277 /* Send feedback to user */
278 CONSOLE_Print("Acceleration X axis=%.3f[g] ",accelX);
279 CONSOLE_Print("Acceleration Y axis=%.3f[g]\r\n",accelY);
280 /* Time delay in order to be able to see the changes in real time. */
281 TIME_DelayMs(200);
282 }
283 }
284
285 /***************************************************************************//**
286 * @brief Displays the tilt on both axes.(40 samples)
287 *
288 * @return None.
289 *******************************************************************************/
290 void GetTilt(double* param, char paramNo) // "tilt?" command
291 {
292 unsigned short ch0RawData = 0;
293 unsigned short ch1RawData = 0;
294 float rawDataX = 0;
295 float rawDataY = 0;
296 float ch0CorrectedData = 0;
297 float ch1CorrectedData = 0;
298 float accelX = 0;
299 float accelY = 0;
300 float asinX = 0;
301 float acosY = 0;
302 unsigned char count = 0;
303
304 for (count = 0; count < 40; count++)
305 {
306 ch0RawData = (AD7887_Read16Bits(dataX)) & 0x0FFF;
307 ch1RawData = (AD7887_Read16Bits(dataY)) & 0x0FFF;
308 /* Be able to have also negative values. */
309 rawDataX = (float)ch0RawData - 2048;
310 rawDataY = (float)ch1RawData - 2048;
311 /* Compute the corrected data using offset and gain. */
312 ch0CorrectedData = (rawDataX - offsetX) * gainX;
313 ch1CorrectedData = (rawDataY - offsetY) * gainY;
314 /* Compute the acceleration values on the X and Y axis. */
315 accelX = ((float)ch0CorrectedData) / ((1 << 11));
316 accelY = ((float)ch1CorrectedData) / ((1 << 11));
317 /* Limit the X acceleration to 1g. */
318 if(fabs(accelX) > 1.0f)
319 {
320 accelX = accelX / fabs(accelX);
321 }
322 /* Limit the Y acceleration to 1g. */
323 if(fabs(accelY) > 1.0f)
324 {
325 accelY = accelY / fabs(accelY);
326 }
327 /* Compute the tilt. */
328 asinX = (float)asin(accelX) / M_PI * 180;
329 acosY = (float)acos(accelY) / M_PI * 180;
330 /* Send feedback to user */
331 CONSOLE_Print("Tilt X axis=%.3f[deg] ",asinX);
332 CONSOLE_Print("Tilt Y axis=%.3f[deg]\r\n",acosY);
333 /* Time delay in order to be able to see the changes in real time. */
334 TIME_DelayMs(200);
335 }
336 }
337
338 /***************************************************************************//**
339 * @brief Acquire data for calibration, when Xacc=+1[g].
340 *
341 * @return None.
342 *******************************************************************************/
343 void GetCalibrationXplus1G(double* param, char paramNo) //"calibX+1G!!" command
344 {
345 unsigned short ch0RawData = 0;
346 unsigned short ch1RawData = 0;
347
348 ch0RawData = (AD7887_Read16Bits(dataX)) & 0x0FFF;
349 ch1RawData = (AD7887_Read16Bits(dataY)) & 0x0FFF;
350
351 samples[0][0] = (float)ch0RawData - 2048;
352 samples[0][1] = (float)ch1RawData - 2048;
353 /* Send feedback to user */
354 CONSOLE_Print("Data was acquired for Xacc=+1[g].\r\n");
355 }
356
357 /***************************************************************************//**
358 * @brief Acquire data for calibration, when Xacc=-1[g].
359 *
360 * @return None.
361 *******************************************************************************/
362 void GetCalibrationXminus1G(double* param, char paramNo) //"calibX-1G!!" command
363 {
364 unsigned short ch0RawData = 0;
365 unsigned short ch1RawData = 0;
366
367 ch0RawData = (AD7887_Read16Bits(dataX)) & 0x0FFF;
368 ch1RawData = (AD7887_Read16Bits(dataY)) & 0x0FFF;
369
370 samples[1][0] = (float)ch0RawData - 2048;
371 samples[1][1] = (float)ch1RawData - 2048;
372 /* Send feedback to user */
373 CONSOLE_Print("Data was acquired for Xacc=-1[g].\r\n");
374 }
375
376 /***************************************************************************//**
377 * @brief Acquire data for calibration, when Yacc=+1[g].
378 *
379 * @return None.
380 *******************************************************************************/
381 void GetCalibrationYplus1G(double* param, char paramNo) //"calibY+1G!!" command
382 {
383 unsigned short ch0RawData = 0;
384 unsigned short ch1RawData = 0;
385
386 ch0RawData = (AD7887_Read16Bits(dataX)) & 0x0FFF;
387 ch1RawData = (AD7887_Read16Bits(dataY)) & 0x0FFF;
388
389 samples[2][0] = (float)ch0RawData - 2048;
390 samples[2][1] = (float)ch1RawData - 2048;
391 /* Send feedback to user */
392 CONSOLE_Print("Data was acquired for Yacc=+1[g].\r\n");
393 }
394 /***************************************************************************//**
395 * @brief Acquire data for calibration, when Yacc=-1[g].
396 *
397 * @return None.
398 *******************************************************************************/
399 void GetCalibrationYminus1G(double* param, char paramNo) //"calibY-1G!!" command
400 {
401 unsigned short ch0RawData = 0;
402 unsigned short ch1RawData = 0;
403
404 ch0RawData = (AD7887_Read16Bits(dataX)) & 0x0FFF;
405 ch1RawData = (AD7887_Read16Bits(dataY)) & 0x0FFF;
406
407 samples[3][0] = (float)ch0RawData - 2048;
408 samples[3][1] = (float)ch1RawData - 2048;
409 /* Send feedback to user */
410 CONSOLE_Print("Data was acquired for Yacc=-1[g].\r\n");
411 }
412
413 /***************************************************************************//**
414 * @brief Calculates the parameters for calibration.
415 *
416 * @return None.
417 *******************************************************************************/
418 void DoCalibrationCalculateParam(double* param, char paramNo) //"calibCalculateParam!" command
419 {
420 /* Compute the new X and Y offsets and gains */
421 offsetX = (samples[2][0] + samples[3][0]) / 2;
422 offsetY = (samples[0][1] + samples[1][1]) / 2;
423 gainX = ((2048.0f / (samples[0][0] - offsetX)) +
424 (-2048.0f / (samples[1][0] - offsetX))) / 2;
425 gainY = ((2048.0f / (samples[2][1] - offsetY)) +
426 (-2048.0f / (samples[3][1] - offsetY))) / 2;
427 /* Send feedback to user */
428 CONSOLE_Print("Offset X axis=%.3f\r\n",offsetX);
429 CONSOLE_Print("Offset Y axis=%.3f\r\n",offsetY);
430 CONSOLE_Print("Gain X axis=%.3f\r\n",gainX);
431 CONSOLE_Print("Gain Y axis=%.3f\r\n",gainY);
432 }