All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
vexime.h
Go to the documentation of this file.
1 /*-----------------------------------------------------------------------------*/
2 /* */
3 /* Copyright (c) James Pearman */
4 /* 2013 */
5 /* All Rights Reserved */
6 /* */
7 /*-----------------------------------------------------------------------------*/
8 /* */
9 /* Module: vexime.h */
10 /* Author: James Pearman */
11 /* Created: 14 May 2013 */
12 /* */
13 /* Revisions: */
14 /* V1.00 4 July 2013 - Initial release */
15 /* */
16 /*-----------------------------------------------------------------------------*/
17 /* */
18 /* This file is part of ConVEX. */
19 /* */
20 /* The author is supplying this software for use with the VEX cortex */
21 /* control system. ConVEX is free software; you can redistribute it */
22 /* and/or modify it under the terms of the GNU General Public License */
23 /* as published by the Free Software Foundation; either version 3 of */
24 /* the License, or (at your option) any later version. */
25 /* */
26 /* ConVEX is distributed in the hope that it will be useful, */
27 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
28 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
29 /* GNU General Public License for more details. */
30 /* */
31 /* You should have received a copy of the GNU General Public License */
32 /* along with this program. If not, see <http://www.gnu.org/licenses/>. */
33 /* */
34 /* A special exception to the GPL can be applied should you wish to */
35 /* distribute a combined work that includes ConVEX, without being obliged */
36 /* to provide the source code for any proprietary components. */
37 /* See the file exception.txt for full details of how and when the */
38 /* exception can be applied. */
39 /* */
40 /* The author can be contacted on the vex forums as jpearman */
41 /* or electronic mail using jbpearman_at_mac_dot_com */
42 /* Mentor for team 8888 RoboLancers, Pasadena CA. */
43 /* */
44 /*-----------------------------------------------------------------------------*/
45 /* */
46 /* Description: */
47 /* */
48 /* This IME version for the ChibiOS/RT implementation on the cortex */
49 /* 14 May 2013 was adapted from the code I wrote for the vexpro in Sept */
50 /* 2012 */
51 /* */
52 /*-----------------------------------------------------------------------------*/
53 /* */
54 
55 #ifndef _VEXIME
56 #define _VEXIME
57 
58 /*-----------------------------------------------------------------------------*/
59 /** @file vexime.h
60  * @brief IME driver macros and prototypes
61 *//*---------------------------------------------------------------------------*/
62 
63 #ifdef __cplusplus
64 extern "C" {
65 #endif
66 
67 /*-----------------------------------------------------------------------------*/
68 /** @brief IME channel definitions */
69 /*-----------------------------------------------------------------------------*/
70 typedef enum {
79 
81 
84 
85 #define IME_MAX kImeTotal ///< Maximum number of IMEs we search for
86 
87 #define DEFAULT_DEVICE 0x60 ///< Default IME address
88 #define IME_START_ADDRESS 0x20 ///< Start address when reassigning
89 
90 #define IME_VERSION_ADDR 0x00 ///< Version request
91 #define IME_VENDOR_ADDR 0x08 ///< Vendor request
92 #define IME_DEVICEID_ADDR 0x10 ///< Device ID request
93 #define IME_STATUS_ADDR 0x20 ///< Status command
94 #define IME_DATA_ADDR 0x40 ///< Get data command
95 
96 #define IME_SCRATCHR_ADDR 0x60 ///< read register offset of scratchpad regs
97 #define IME_SCRATCHW_ADDR 0xA0 ///< write register offset of scratchpad regs
98 
99 #define IME_CLEAR_COUNTERS 0x4A ///< Clear counters command
100 #define IME_DISABLE_TERM 0x4B ///< disable termination command
101 #define IME_ENABLE_TERM 0x4C ///< enable termination command
102 #define IME_SETDEV_REG 0x4D ///< Set device address command
103 #define IME_RESET_REG 0x4E ///< Reset command
104 
105 #define IME_BUF_LEN 16
106 
107 #define ACTION_POLL 0
108 #define ACTION_RENEGOTIATE 1
109 
110 /*-----------------------------------------------------------------------------*/
111 /** @brief structure to hold everything for one encoder */
112 /*-----------------------------------------------------------------------------*/
113 typedef struct _imeData {
114  uint8_t address; ///< The I2C address of this IME
115  uint8_t type; ///< The type of motor this IME is attached to
116  uint16_t valid; ///< The IME has been negotiated with and is valid
117  uint16_t motor_index; ///< A reference to the motor that this IME is connected to
118 
119 
120  uint8_t version[IME_BUF_LEN]; /**< @brief buffer to store version string
121  read from this IME */
122  uint8_t vendor[IME_BUF_LEN]; /**< @brief buffer to store vendor string
123  read from this IME */
124  uint8_t deviceid[IME_BUF_LEN]; /**< @brief buffer to store device id string
125  read from this IME */
126  uint8_t enc_data[IME_BUF_LEN]; /**< @brief buffer used when reading encoder
127  count */
128 
129  int32_t count; ///< last encoder count read from IME
130  int32_t offset; ///< an offset that id deducted from count
131  int32_t velocity; ///< velocity data from IME
132  int32_t delta_count; ///< change in count from last time read
133  int32_t rpm; ///< calculated rpm (not tested yet)
134 
135  int32_t old_count; ///< count from last poll
136 
137  int32_t rpm_constant; ///< constant used to calculate rpm based on IME type
138 
139  // track data requests and errors
140  uint32_t data_polls; ///< number of times this IME was polled
141  uint32_t data_errors; ///< number of errors in communication with this IME
142  } imeData;
143 
144 /*-----------------------------------------------------------------------------*/
145 /** @brief structure holding all IME data */
146 /*-----------------------------------------------------------------------------*/
147 typedef struct _vexImeData {
148  I2CDriver *i2cp; ///< pointer to the I2C driver
149  vexStream *chp; ///< Pointer to vexStream for debug output
150  int16_t num; ///< the number of IMEs found on the I2C bus
151  uint16_t nextAddress; ///< address for the next discovered IME
152  uint16_t error_lockup; ///< total bus loss errors
153  uint16_t error_ack; ///< acknowledge errors
154  uint16_t error_bus; ///< bus errors
155  uint16_t error_arb; ///< arbitration errors
156  uint16_t error_tim; ///< timing errors
157  uint16_t error_seq; ///< sequential errors
158  uint16_t action; ///< indicates next action the IME thread should take
159  uint16_t debug; ///< flag indicates verbose debug output
160  imeData imes[ IME_MAX ];///< array with data for each IME
161  } vexImeData;
162 
163 #define IME_COUNT_RESET 0x7F000000 ///< Dummy value used during IME reset
164 
165 
166 /** @name IME motor types
167  * @{
168  */
169 #define IME_269 0
170 #define IME_393T 1
171 #define IME_393S 2
172 #define IME_UND 16
173 /** @} */
174 
175 /*-----------------------------------------------------------------------------*/
176 int32_t vexImeGetCount( int16_t channel );
177 void vexImeSetCount( int16_t channel, int32_t value );
178 int16_t vexImeGetId( int16_t channel );
179 int16_t vexImeGetChannelMax(void);
180 
182 
183 void vexImeInit( I2CDriver *i2cp, vexStream *chp );
184 void vexImeSetType( tVexImeChannels channel, tVexMotorType type );
185 void vexImeSetMotor( tVexImeChannels channel, tVexMotor motor );
186 int32_t vexImeMotorGetCount( tVexMotor motor );
187 
188 // All of these functions are internal driver use
189 // we expose them so alternative IME drivers could be written
190 uint16_t vexIMEFindEncoders( void );
191 void vexIMEDataInit( imeData *ime );
192 
193 msg_t vexIMEUpdateCounts( imeData *ime );
194 
195 msg_t vexIMEGetVersion( uint8_t device, uint8_t *buf );
196 msg_t vexIMEGetVendor( uint8_t device, uint8_t *buf );
197 msg_t vexIMEGetDeviceId( uint8_t device, uint8_t *buf );
198 msg_t vexIMEGetStatus( uint8_t device, uint8_t *buf );
199 msg_t vexIMEGetData( uint8_t device, uint8_t *buf );
200 msg_t vexIMEGetScratchpad( uint8_t device, uint8_t *buf, uint8_t offset, uint8_t len );
201 msg_t vexIMESetScratchpad( uint8_t device, uint8_t *buf, uint8_t offset, uint8_t len );
202 msg_t vexIMEClearCounters( uint8_t device );
203 msg_t vexIMEDisableTermination( uint8_t device );
204 msg_t vexIMEEnableTermination( uint8_t device );
205 msg_t vexIMESetAddr( uint8_t *device, uint8_t newDevice );
206 msg_t vexIMEResetAll( void );
207 
208 void vexIMEHandleErrors( msg_t status, i2cflags_t errors );
209 void vexIMEPrintBuf( vexStream *chp, uint8_t *buf, uint8_t len );
210 void vexIMEDebug(vexStream *chp, int argc, char *argv[]);
211 
212 #ifdef __cplusplus
213 } // Close scope of extern "C" declaration
214 #endif
215 
216 #endif /* _VEXIME */