| 1 | /**************************************************************************//** |
| 2 | * @file Commanad.c |
| 3 | * @brief Implementation of the commands given by user through UART for AD2S1205. |
| 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 "ad2s1205.h" |
| 48 | |
| 49 | /******************************************************************************/ |
| 50 | /************************ Constants Definitions *******************************/ |
| 51 | /******************************************************************************/ |
| 52 | |
| 53 | const struct cmd_info cmdList[] = { |
| 54 | [0] = { |
| 55 | .name = "help?", |
| 56 | .description = "Displays all available commands.", |
| 57 | .acceptedValue = "", |
| 58 | .example = "", |
| 59 | }, |
| 60 | [1] = { |
| 61 | .name = "position?", |
| 62 | .description = "Displays the current angular position.", |
| 63 | .acceptedValue = "", |
| 64 | .example = "", |
| 65 | }, |
| 66 | [2] = { |
| 67 | .name = "velocity?", |
| 68 | .description = "Displays the current angular velocity.", |
| 69 | .acceptedValue = "", |
| 70 | .example = "", |
| 71 | }, |
| 72 | }; |
| 73 | |
| 74 | const char cmdNo = (sizeof(cmdList) / sizeof(struct cmd_info)); |
| 75 | const float TC = 0.001f; |
| 76 | |
| 77 | /******************************************************************************/ |
| 78 | /************************ Variables Definitions *******************************/ |
| 79 | /******************************************************************************/ |
| 80 | cmdFunction cmdFunctions[] = {GetHelp, GetPosition, GetVelocity}; |
| 81 | |
| 82 | /* Variables holding information about the device */ |
| 83 | unsigned short position = 0; |
| 84 | unsigned short maxPosition = 0; |
| 85 | unsigned short minPosition = 0; |
| 86 | float meanPosition = 0; |
| 87 | short velocity = 0; |
| 88 | short maxVelocity = 0; |
| 89 | short minVelocity = 0; |
| 90 | float meanVelocity = 0; |
| 91 | |
| 92 | |
| 93 | /***************************************************************************//** |
| 94 | * @brief Displays all available commands. |
| 95 | * |
| 96 | * @return None. |
| 97 | *******************************************************************************/ |
| 98 | void GetHelp(double* param, char paramNo) // "help?" command |
| 99 | { |
| 100 | unsigned char displayCmd; |
| 101 | |
| 102 | CONSOLE_Print("Available commands:\r\n"); |
| 103 | for(displayCmd = 0; displayCmd < cmdNo; displayCmd++) |
| 104 | { |
| 105 | CONSOLE_Print("%s - %s %s\r\n", (char*)cmdList[displayCmd].name, |
| 106 | (char*)cmdList[displayCmd].description, |
| 107 | (char*)cmdList[displayCmd].acceptedValue); |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | /***************************************************************************//** |
| 112 | * @brief Internal function for displaying all the command with its description. |
| 113 | * |
| 114 | * @return None. |
| 115 | *******************************************************************************/ |
| 116 | void DisplayCmdList() |
| 117 | { |
| 118 | unsigned char displayCmd; |
| 119 | |
| 120 | for(displayCmd = 0; displayCmd < cmdNo; displayCmd++) |
| 121 | { |
| 122 | CONSOLE_Print("%s - %s\r\n", (char*)cmdList[displayCmd].name, \ |
| 123 | (char*)cmdList[displayCmd].description); |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | /***************************************************************************//** |
| 128 | * @brief Displays error message. |
| 129 | * |
| 130 | * @return None. |
| 131 | *******************************************************************************/ |
| 132 | void DisplayError(unsigned char funcNo) |
| 133 | { |
| 134 | /* Display error messages */ |
| 135 | CONSOLE_Print("Invalid parameter!\r\n"); |
| 136 | CONSOLE_Print("%s - %s %s\r\n", (char*)cmdList[funcNo].name, \ |
| 137 | (char*)cmdList[funcNo].description, \ |
| 138 | (char*)cmdList[funcNo].acceptedValue); |
| 139 | CONSOLE_Print("Example: %s\r\n", (char*)cmdList[funcNo].example); |
| 140 | } |
| 141 | |
| 142 | /***************************************************************************//** |
| 143 | * @brief Initializes the device. |
| 144 | * |
| 145 | * @return - The result of the initialization. |
| 146 | * Example: ERROR - the device was not initialized or the device |
| 147 | * is not present. |
| 148 | * SUCCES - the device was initialized and the device |
| 149 | * is present. |
| 150 | *******************************************************************************/ |
| 151 | char DoDeviceInit(void) |
| 152 | { |
| 153 | if(AD2S1205_Init() == 0) |
| 154 | { |
| 155 | CONSOLE_Print("Device OK\r\n"); |
| 156 | DisplayCmdList(); |
| 157 | return SUCCESS; |
| 158 | } |
| 159 | else |
| 160 | { |
| 161 | CONSOLE_Print("Device Error\r\n"); |
| 162 | return ERROR; |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | /***************************************************************************//** |
| 167 | * @brief Displays the current angular position. |
| 168 | * |
| 169 | * @return None. |
| 170 | *******************************************************************************/ |
| 171 | void GetPosition(double* param, char paramNo) // "position?" command |
| 172 | { |
| 173 | u32 count; |
| 174 | |
| 175 | AD2S1205_Pos_Vel(&position, &velocity); |
| 176 | maxPosition = position; |
| 177 | minPosition = position; |
| 178 | |
| 179 | for(count = 0; count < 2000; count++) |
| 180 | { |
| 181 | AD2S1205_Pos_Vel(&position, &velocity); |
| 182 | /* Compute the min, max and average position values */ |
| 183 | if(position > maxPosition) |
| 184 | { |
| 185 | maxPosition = position; |
| 186 | } |
| 187 | if(position < minPosition) |
| 188 | { |
| 189 | minPosition = position; |
| 190 | } |
| 191 | meanPosition = meanPosition * (1-TC) + (float)position * TC; |
| 192 | } |
| 193 | /* Send the requested value to user */ |
| 194 | CONSOLE_Print("Position = %d\r\n", position); |
| 195 | CONSOLE_Print("Min Position = %d\r\n", minPosition); |
| 196 | CONSOLE_Print("Max Position = %d\r\n", maxPosition); |
| 197 | CONSOLE_Print("Mean Position = %.3f\r\n", meanPosition); |
| 198 | } |
| 199 | |
| 200 | /***************************************************************************//** |
| 201 | * @brief Displays the current angular velocity. |
| 202 | * |
| 203 | * @return None. |
| 204 | *******************************************************************************/ |
| 205 | void GetVelocity(double* param, char paramNo) // "velocity?" command |
| 206 | { |
| 207 | u32 count; |
| 208 | |
| 209 | AD2S1205_Pos_Vel(&position, &velocity); |
| 210 | maxVelocity = velocity; |
| 211 | minVelocity = velocity; |
| 212 | |
| 213 | for(count = 0; count < 2000; count++) |
| 214 | { |
| 215 | AD2S1205_Pos_Vel(&position, &velocity); |
| 216 | /* Compute the min, max and average velocity values */ |
| 217 | if(velocity > maxVelocity) |
| 218 | { |
| 219 | maxVelocity = velocity; |
| 220 | } |
| 221 | if(velocity < minVelocity) |
| 222 | { |
| 223 | minVelocity = velocity; |
| 224 | } |
| 225 | meanVelocity = meanVelocity * (1-TC) + (float)velocity * TC; |
| 226 | } |
| 227 | /* Send the requested value to user */ |
| 228 | CONSOLE_Print("Velocity = %d\r\n", velocity); |
| 229 | CONSOLE_Print("Min Velocity = %d\r\n", minVelocity); |
| 230 | CONSOLE_Print("Max Velocity = %d\r\n", maxVelocity); |
| 231 | CONSOLE_Print("Mean Velocity = %.3f\r\n", meanVelocity); |
| 232 | } |