00001 /* 00002 * Copyright (C) 2007, 2009 Austin Robot Technology 00003 * 00004 * License: Modified BSD Software License Agreement 00005 * 00006 * $Id: avr_controller.h 18 2010-01-24 14:31:49Z jack.oquin $ 00007 */ 00008 00009 #ifndef _AVR_CONTROLLER_H 00010 #define _AVR_CONTROLLER_H 00011 00120 #include <netinet/in.h> /* byte-reversal functions */ 00121 00122 /* typedefs for fixed-size fields from standard C types */ 00123 #include <inttypes.h> 00124 typedef int8_t s08; 00125 typedef int16_t s16; 00126 typedef uint8_t u08; 00127 typedef uint16_t u16; 00128 typedef uint32_t u32; 00129 00130 #include "art_command_protocol.h" 00131 00132 #define AVR_POS_ABSURD 7 /* absurdly small position value */ 00133 00134 /* warning! this is GCC-specific. */ 00135 #pragma pack(push,1) /* ensure compiler packs this struct */ 00136 00137 struct avr_cmd { 00138 uint8_t comlen; /* command and length (4 bits each) */ 00139 uint8_t seq; /* sequence number */ 00140 union { 00141 uint8_t data08; /* 8-bit data field */ 00142 uint16_t data16; /* 16-bit data field */ 00143 uint32_t data32; /* 32-bit data field */ 00144 struct { /* status response data */ 00145 uint8_t pos; /* throttle position */ 00146 uint16_t rpms; /* engine RPMs */ 00147 uint8_t diagn; /* diagnostic flag byte */ 00148 uint8_t direction; /* motor direction: 1=fwd, 0=rev */ 00149 int16_t dstate; /* derivative state */ 00150 int16_t istate; /* integral state */ 00151 uint16_t pwm_count; /* motor output */ 00152 } status; 00153 } data; 00154 /* checksum follows last data byte at offset (len-1) */ 00155 uint8_t csum; /* reserve space for it */ 00156 }; 00157 00158 #pragma pack(pop) /* restore default struct packing */ 00159 00160 /* returns the offset of "field" in the AVR command structure */ 00161 #define avr_offset(field) \ 00162 ((size_t) (((char *)(&(((struct avr_cmd*)NULL)->field))) - ((char *)NULL))) 00163 00164 /* 00165 * Accessor functions for packed bit fields. Trusting the compiler to 00166 * pack them correctly does not work and is non-portable, anyway. 00167 */ 00168 static inline void avr_set_com_len(struct avr_cmd *cmd, int com, int len) 00169 {cmd->comlen = (com << 4) | len;} 00170 00171 static inline uint8_t avr_get_com(struct avr_cmd *cmd) 00172 {return (cmd->comlen >> 4) & 0x0F;} 00173 00174 static inline uint8_t avr_get_len(struct avr_cmd *cmd) 00175 {return cmd->comlen & 0x0F;} 00176 00177 static inline uint8_t avr_get_pos(struct avr_cmd *status) 00178 {return status->data.status.pos;} 00179 00180 static inline uint16_t avr_get_rpms(struct avr_cmd *status) 00181 {return status->data.status.rpms;} 00182 00183 static inline bool avr_get_gen(struct avr_cmd *status) 00184 {return status->data.status.diagn & 0x01;} 00185 00186 static inline bool avr_get_pvld(struct avr_cmd *status) 00187 {return status->data.status.diagn>>1 & 0x01;} 00188 00189 static inline bool avr_get_rvld(struct avr_cmd *status) 00190 {return status->data.status.diagn>>2 & 0x01;} 00191 00192 static inline bool avr_get_estop(struct avr_cmd *status) 00193 {return status->data.status.diagn>>3 & 0x01;} 00194 00195 static inline uint8_t avr_get_diag(struct avr_cmd *status) 00196 {return status->data.status.diagn>>4 & 0x0F;} 00197 00198 static inline uint8_t avr_get_direction(struct avr_cmd *status) 00199 {return status->data.status.direction;} 00200 00201 static inline int16_t avr_get_dstate(struct avr_cmd *status) 00202 {return ntohs(status->data.status.dstate);} 00203 00204 static inline int16_t avr_get_istate(struct avr_cmd *status) 00205 {return ntohs(status->data.status.istate);} 00206 00207 static inline int16_t avr_get_pwm(struct avr_cmd *status) 00208 {return ntohs(status->data.status.pwm_count);} 00209 00210 00211 #endif // _AVR_CONTROLLER_H