00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 #ifndef __MSIMU_H__
00013 #define __MSIMU_H__
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037 #pragma systemFile
00038
00039 #ifndef __COMMON_H__
00040 #include "common.h"
00041 #endif
00042
00043 #define MSIMU_IMU_I2C_ADDR 0x22
00044
00045 #define MSIMU_REG_CMD 0x41
00046
00047 #define MSIMU_REG_TILT_ALL_AXES 0x42
00048 #define MSIMU_REG_ACC_ALL_AXES 0x45
00049 #define MSIMU_REG_COMPASS_HEADING 0x4B
00050 #define MSIMU_REG_COMPASS_ALL_FIELDS 0x4D
00051 #define MSIMU_REG_GYRO_ALL_AXES 0x53
00052 #define MSIMU_REG_GYRO_FILTER 0x5A
00053
00054 #define MSIMU_CMD_COMPASS_START_CAL 0x43
00055 #define MSIMU_CMD_COMPASS_STOP_CAL 0x63
00056
00057 #define MSIMU_CMD_ACC_RANGE_2G 0x31
00058 #define MSIMU_CMD_ACC_RANGE_4G 0x32
00059 #define MSIMU_CMD_ACC_RANGE_8G 0x33
00060 #define MSIMU_CMD_ACC_RANGE_16G 0x34
00061
00062 #define MSIMU_GYRO_FILTER_NONE 0
00063 #define MSIMU_GYRO_FILTER_LEVEL_1 1
00064 #define MSIMU_GYRO_FILTER_LEVEL_2 2
00065 #define MSIMU_GYRO_FILTER_LEVEL_3 3
00066 #define MSIMU_GYRO_FILTER_LEVEL_4 4
00067 #define MSIMU_GYRO_FILTER_LEVEL_5 5
00068 #define MSIMU_GYRO_FILTER_LEVEL_6 6
00069 #define MSIMU_GYRO_FILTER_LEVEL_7 7
00070
00071
00072 #define MSIMU_TILT_X_AXIS MSIMU_REG_TILT_X_AXIS
00073 #define MSIMU_TILT_Y_AXIS MSIMU_REG_TILT_Y_AXIS
00074 #define MSIMU_TILT_Z_AXIS MSIMU_REG_TILT_Z_AXIS
00075
00076 #define MSIMU_ACC_X_AXIS MSIMU_REG_ACC_X_AXIS
00077 #define MSIMU_ACC_Y_AXIS MSIMU_REG_ACC_Y_AXIS
00078 #define MSIMU_ACC_Z_AXIS MSIMU_REG_ACC_Z_AXIS
00079
00080 #define MSIMU_GYRO_X_AXIS MSIMU_REG_GYRO_X_AXIS
00081 #define MSIMU_GYRO_Y_AXIS MSIMU_REG_GYRO_Y_AXIS
00082 #define MSIMU_GYRO_Z_AXIS MSIMU_REG_GYRO_Z_AXIS
00083
00084 tByteArray MSIMU_I2CRequest;
00085 tByteArray MSIMU_I2CReply;
00086
00087 bool _MSIMUsendCMD(tSensors link, ubyte cmd);
00088 bool MSIMUreadTiltAxes(tSensors link, int &_x, int &_y, int &_z);
00089 bool MSIMUreadGyroAxes(tSensors link, int &_x, int &_y, int &_z);
00090 bool MSIMUreadAccelAxes(tSensors link, int &_x, int &_y, int &_z);
00091 bool MSIMUreadMagneticFields(tSensors link, int &_x, int &_y, int &_z);
00092 int MSIMUreadHeading(tSensors link);
00093 bool MSIMUsetGyroFilter(tSensors link, ubyte level);
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104 bool _MSIMUsendCMD(tSensors link, ubyte cmd)
00105 {
00106 MSIMU_I2CRequest[0] = 3;
00107 MSIMU_I2CRequest[1] = MSIMU_IMU_I2C_ADDR;
00108 MSIMU_I2CRequest[2] = MSIMU_REG_CMD;
00109 MSIMU_I2CRequest[3] = cmd;
00110
00111 return writeI2C(link, MSIMU_I2CRequest);
00112 }
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123 bool MSIMUreadTiltAxes(tSensors link, int &_x, int &_y, int &_z){
00124 MSIMU_I2CRequest[0] = 2;
00125 MSIMU_I2CRequest[1] = MSIMU_IMU_I2C_ADDR;
00126 MSIMU_I2CRequest[2] = MSIMU_REG_TILT_ALL_AXES;
00127
00128 if (!writeI2C(link, MSIMU_I2CRequest, MSIMU_I2CReply, 3))
00129 return false;
00130
00131 _x = (MSIMU_I2CReply[0] >= 128) ? (int)MSIMU_I2CReply[0] - 256 : (int)MSIMU_I2CReply[0];
00132 _y = (MSIMU_I2CReply[1] >= 128) ? (int)MSIMU_I2CReply[1] - 256 : (int)MSIMU_I2CReply[1];
00133 _z = (MSIMU_I2CReply[2] >= 128) ? (int)MSIMU_I2CReply[2] - 256 : (int)MSIMU_I2CReply[2];
00134
00135 return true;
00136 }
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147 bool MSIMUreadGyroAxes(tSensors link, int &_x, int &_y, int &_z){
00148 MSIMU_I2CRequest[0] = 2;
00149 MSIMU_I2CRequest[1] = MSIMU_IMU_I2C_ADDR;
00150 MSIMU_I2CRequest[2] = MSIMU_REG_GYRO_ALL_AXES;
00151
00152 if (!writeI2C(link, MSIMU_I2CRequest, MSIMU_I2CReply, 6))
00153 return false;
00154
00155 _x = MSIMU_I2CReply[0] + ((int)(MSIMU_I2CReply[1]<<8));
00156 _y = MSIMU_I2CReply[2] + ((int)(MSIMU_I2CReply[3]<<8));
00157 _z = MSIMU_I2CReply[4] + ((int)(MSIMU_I2CReply[5]<<8));
00158 return true;
00159 }
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170 bool MSIMUreadAccelAxes(tSensors link, int &_x, int &_y, int &_z){
00171 MSIMU_I2CRequest[0] = 2;
00172 MSIMU_I2CRequest[1] = MSIMU_IMU_I2C_ADDR;
00173 MSIMU_I2CRequest[2] = MSIMU_REG_ACC_ALL_AXES;
00174
00175 if (!writeI2C(link, MSIMU_I2CRequest, MSIMU_I2CReply, 6))
00176 return false;
00177
00178 _x = MSIMU_I2CReply[0] + ((int)(MSIMU_I2CReply[1]<<8));
00179 _y = MSIMU_I2CReply[2] + ((int)(MSIMU_I2CReply[3]<<8));
00180 _z = MSIMU_I2CReply[4] + ((int)(MSIMU_I2CReply[5]<<8));
00181 return true;
00182 }
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194 bool MSIMUreadMagneticFields(tSensors link, int &_x, int &_y, int &_z){
00195 MSIMU_I2CRequest[0] = 2;
00196 MSIMU_I2CRequest[1] = MSIMU_IMU_I2C_ADDR;
00197 MSIMU_I2CRequest[2] = MSIMU_REG_ACC_ALL_AXES;
00198
00199 if (!writeI2C(link, MSIMU_I2CRequest, MSIMU_I2CReply, 6))
00200 return false;
00201
00202 _x = MSIMU_I2CReply[0] + ((int)(MSIMU_I2CReply[1]<<8));
00203 _y = MSIMU_I2CReply[2] + ((int)(MSIMU_I2CReply[3]<<8));
00204 _z = MSIMU_I2CReply[4] + ((int)(MSIMU_I2CReply[5]<<8));
00205
00206 return true;
00207 }
00208
00209
00210
00211
00212
00213
00214
00215 int MSIMUreadHeading(tSensors link)
00216 {
00217 MSIMU_I2CRequest[0] = 2;
00218 MSIMU_I2CRequest[1] = MSIMU_IMU_I2C_ADDR;
00219 MSIMU_I2CRequest[2] = MSIMU_REG_COMPASS_HEADING;
00220
00221 if (!writeI2C(link, MSIMU_I2CRequest, MSIMU_I2CReply, 2))
00222 return 0;
00223
00224 return MSIMU_I2CReply[0] + ((int)(MSIMU_I2CReply[1]<<8));
00225 }
00226
00227
00228
00229
00230
00231
00232
00233
00234 bool MSIMUsetGyroFilter(tSensors link, ubyte level)
00235 {
00236 MSIMU_I2CRequest[0] = 3;
00237 MSIMU_I2CRequest[1] = MSIMU_IMU_I2C_ADDR;
00238 MSIMU_I2CRequest[2] = MSIMU_REG_GYRO_FILTER;
00239 MSIMU_I2CRequest[3] = level;
00240
00241 return writeI2C(link, MSIMU_I2CRequest);
00242 }
00243
00244 #endif // __MSIMU_H__
00245
00246
00247
00248
00249
00250