00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 #ifndef __NXTSERVO_H__
00013 #define __NXTSERVO_H__
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 #pragma systemFile
00036
00037 #ifndef __COMMON_H__
00038 #include "common.h"
00039 #endif
00040
00041 #define NXTSERVO_I2C_ADDR 0xB0
00042
00043
00044 #define NXTSERVO_CMD 0x41
00045
00046
00047 #define NXTSERVO_POS_CHAN1 0x42
00048 #define NXTSERVO_POS_CHAN2 0x44
00049 #define NXTSERVO_POS_CHAN3 0x46
00050 #define NXTSERVO_POS_CHAN4 0x48
00051 #define NXTSERVO_POS_CHAN5 0x4A
00052 #define NXTSERVO_POS_CHAN6 0x4C
00053 #define NXTSERVO_POS_CHAN7 0x4E
00054 #define NXTSERVO_POS_CHAN8 0x50
00055
00056
00057 #define NXTSERVO_SPEED_CHAN1 0x52
00058 #define NXTSERVO_SPEED_CHAN2 0x53
00059 #define NXTSERVO_SPEED_CHAN3 0x54
00060 #define NXTSERVO_SPEED_CHAN4 0x55
00061 #define NXTSERVO_SPEED_CHAN5 0x56
00062 #define NXTSERVO_SPEED_CHAN6 0x57
00063 #define NXTSERVO_SPEED_CHAN7 0x58
00064 #define NXTSERVO_SPEED_CHAN8 0x59
00065
00066
00067 #define NXTSERVO_QPOS_CHAN1 0x42
00068 #define NXTSERVO_QPOS_CHAN2 0x44
00069 #define NXTSERVO_QPOS_CHAN3 0x46
00070 #define NXTSERVO_QPOS_CHAN4 0x48
00071 #define NXTSERVO_QPOS_CHAN5 0x4A
00072 #define NXTSERVO_QPOS_CHAN6 0x4C
00073 #define NXTSERVO_QPOS_CHAN7 0x4E
00074 #define NXTSERVO_QPOS_CHAN8 0x50
00075
00076
00077 #define NXTSERVO_MIN_POS 500
00078 #define NXTSERVO_MAX_POS 2500
00079 #define NXTSERVO_MID_POS 1500
00080
00081
00082
00083
00084 bool NXTServoSetSpeed(tSensors link, ubyte servochan, ubyte speed, ubyte address);
00085 bool NXTServoSetPos(tSensors link, ubyte servochan, int position, ubyte speed, ubyte address = NXTSERVO_I2C_ADDR);
00086 bool NXTServoQSetPos(tSensors link, ubyte servochan, ubyte position, byte speed, ubyte address = NXTSERVO_I2C_ADDR);
00087 int NXTServoReadVoltage(tSensors link, ubyte address = NXTSERVO_I2C_ADDR);
00088
00089 tByteArray NXTSERVO_I2CRequest;
00090 tByteArray NXTSERVO_I2CReply;
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104 bool NXTServoSetSpeed(tSensors link, ubyte servochan, ubyte speed, ubyte address) {
00105 memset(NXTSERVO_I2CRequest, 0, sizeof(tByteArray));
00106 NXTSERVO_I2CRequest[0] = 3;
00107 NXTSERVO_I2CRequest[1] = address;
00108 NXTSERVO_I2CRequest[2] = NXTSERVO_SPEED_CHAN1 + (servochan - 1);
00109 NXTSERVO_I2CRequest[3] = speed;
00110
00111 return writeI2C(link, NXTSERVO_I2CRequest);
00112 }
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124 bool NXTServoSetPos(tSensors link, ubyte servochan, int position, ubyte speed, ubyte address) {
00125 memset(NXTSERVO_I2CRequest, 0, sizeof(tByteArray));
00126 if (!NXTServoSetSpeed(link, servochan, speed, address))
00127 return false;
00128
00129 position = clip(position, 500, 2500);
00130
00131
00132 memset(NXTSERVO_I2CRequest, 0, sizeof(tByteArray));
00133 NXTSERVO_I2CRequest[0] = 4;
00134 NXTSERVO_I2CRequest[1] = address;
00135 NXTSERVO_I2CRequest[2] = NXTSERVO_POS_CHAN1 + ((servochan - 1) * 2);
00136 NXTSERVO_I2CRequest[3] = position & 0x00FF;
00137 NXTSERVO_I2CRequest[4] = (position >> 8) & 0x00FF;
00138
00139 return writeI2C(link, NXTSERVO_I2CRequest);
00140 }
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155 bool NXTServoQSetPos(tSensors link, ubyte servochan, ubyte position, byte speed, ubyte address) {
00156 memset(NXTSERVO_I2CRequest, 0, sizeof(tByteArray));
00157
00158 position = clip(position, 50, 250);
00159
00160 if (!NXTServoSetSpeed(link, servochan, speed, address))
00161 return false;
00162
00163
00164
00165 NXTSERVO_I2CRequest[0] = 3;
00166 NXTSERVO_I2CRequest[1] = address;
00167 NXTSERVO_I2CRequest[2] = NXTSERVO_QPOS_CHAN1 + (servochan - 1);
00168 NXTSERVO_I2CRequest[3] = position;
00169
00170 return writeI2C(link, NXTSERVO_I2CRequest);
00171 }
00172
00173
00174
00175
00176
00177
00178
00179
00180 int NXTServoReadVoltage(tSensors link, ubyte address) {
00181 long mvs = 0;
00182
00183 memset(NXTSERVO_I2CRequest, 0, sizeof(tByteArray));
00184
00185 NXTSERVO_I2CRequest[0] = 2;
00186 NXTSERVO_I2CRequest[1] = address;
00187 NXTSERVO_I2CRequest[2] = NXTSERVO_CMD;
00188
00189 if (!writeI2C(link, NXTSERVO_I2CRequest, NXTSERVO_I2CReply, 1))
00190 return -1;
00191
00192 mvs = ((long)NXTSERVO_I2CReply[0] * 3886) / 100;
00193
00194 return mvs;
00195 }
00196
00197 #endif // __NXTSERVO_H__
00198
00199
00200
00201
00202