I remember all the talk Ford was a part of, in regard to NXC drivers, so I was a tad bit concerned about getting the controllers to work with NXC. However, I was very happily surprised to have all fears shattered by nice, solid drivers within only a couple hours (including lots of testing ).
I posted a (fairly long) article about it on my blog. I have the library, and well as an example posted in the article, but I'll post it here anyhow. Here is the library:
Code: Select all
#define TETRIX_ADDRESS_1 0x02 //Daisey chain position 1
#define TETRIX_ADDRESS_2 0x04 //Daisey chain position 2
#define TETRIX_ADDRESS_3 0x06 //Daisey chain position 3
#define TETRIX_ADDRESS_4 0x08 //Daisey chain position 4
#define TETRIX_MOTOR_BRAKE 0 //Motor brake
#define TETRIX_MOTOR_FLOAT -128 //0x80, Motor float
#define TETRIX_MOTOR_REV 0x08 //Reverse bit
#define TETRIX_SERVO_TIME_RESET 0x00 //Restart 10 second timer
#define TETRIX_SERVO_OFF 0xFF //Turn off/float all servos
#define TETRIX_SERVO_STAY_ON 0xAA //Disable 10 second timer
void TetrixI2CWrite(byte port, byte addr, byte reg, byte data[])
{
byte cmdbuf[]; //register data
int loop, n, nByteReady;
ArrayBuild(cmdbuf, addr, reg, data);
loop = STAT_COMM_PENDING; //Wait until bus is free
while (loop == STAT_COMM_PENDING ){ // ''
loop = I2CStatus(port, nByteReady); // ''
} // ''
n = I2CWrite(port, 0, cmdbuf); //When the I2C bus is ready, send the message you built
while (I2CStatus(port, nByteReady) == STAT_COMM_PENDING); //Wait until bus is free
}
inline void TetrixSetup(byte port){
asm{
setin IN_TYPE_LOWSPEED , port, TypeField // IN_TYPE_LOWSPEED (not 9v)
setin IN_TYPE_NO_SENSOR , port, InputModeField
setin IN_TYPE_SWITCH, port, InvalidDataField
SensorStillInvalid:
getin __ResetSensorTmp, port, InvalidDataField
brtst NEQ, SensorStillInvalid, __ResetSensorTmp
};
}
char TetrixMotorControlData[4];
void TetrixMotors(byte port, byte addr, char motor1, char motor2, byte options1 = 0, byte options2 = 0){
TetrixMotorControlData[0]=options1;
TetrixMotorControlData[3]=options2;
TetrixMotorControlData[1]=motor1;
TetrixMotorControlData[2]=motor2;
TetrixI2CWrite(port, addr, 0x44, TetrixMotorControlData);
}
byte TetrixServoControlData[8];
void TetrixServos(byte port, byte addr, byte servo1, byte servo2, byte servo3, byte servo4, byte servo5, byte servo6, byte step_time = 0, byte pwm = TETRIX_SERVO_TIME_RESET){
TetrixServoControlData[0]=step_time;
TetrixServoControlData[1]=servo1;
TetrixServoControlData[2]=servo2;
TetrixServoControlData[3]=servo3;
TetrixServoControlData[4]=servo4;
TetrixServoControlData[5]=servo5;
TetrixServoControlData[6]=servo6;
TetrixServoControlData[7]=pwm;
TetrixI2CWrite(port, addr, 0x41, TetrixServoControlData);
}
Code: Select all
#include "MyTetrix lib.nxc"
#define TETRIX_PORT S1
char Motor1;
char Motor2;
int Servo1, Servo2, Servo3, Servo4, Servo5, Servo6;
task main(){
TetrixSetup(TETRIX_PORT);
while(true){
Motor1=Random(200)-100;
Motor2=Random(200)-100;
TetrixMotors(TETRIX_PORT, TETRIX_ADDRESS_1, Motor1, Motor2, 0x00, TETRIX_MOTOR_REV);
Servo1=Random(200)+27;
Servo2=Random(200)+27;
Servo3=Random(200)+27;
Servo4=Random(200)+27;
Servo5=Random(200)+27;
Servo6=Random(200)+27;
TetrixServos(TETRIX_PORT, TETRIX_ADDRESS_2, Servo1, Servo2, Servo3, Servo4, Servo5, Servo6, 0, TETRIX_SERVO_TIME_RESET);
ClearScreen();
NumOut(0, LCD_LINE1, Motor1);
NumOut(0, LCD_LINE2, Motor2);
NumOut(0, LCD_LINE3, Servo1);
NumOut(0, LCD_LINE4, Servo2);
NumOut(0, LCD_LINE5, Servo3);
NumOut(0, LCD_LINE6, Servo4);
NumOut(0, LCD_LINE7, Servo5);
NumOut(0, LCD_LINE8, Servo6);
Wait(1000);
}
}
If you want to actually use the drivers, please read the entire article. I explain many things in it.Matt's Blog wrote:The example is just a while(true) loop, that sets the motors' and servos' parameters to random values every second (plus code run time). The example program, is just that, an example. It's just a PoC (Proof of Concept) program.
To use the example, connect the motor controller to input port 1 of the NXT, and daisy chain the servo controller to the other port of the motor controller. Make sure that the motors can physically do anything (because of the Random values), and that you have the controllers connected to the 12v battery (and the switch is on).
If you have any questions, please let me know.
@Ford, if you want me to, I could try to (blindly) add support for the encoders. I don't have any Tetrix encoders though, so testing on my own will be impossible.
I done wrote:Way OT, but I didn't know you could use a URL as the origin of a quote... cool!