NXC: problem with reading out the servo position
Posted: 23 Aug 2013, 14:30
Hi,
I have several Hitec Servos connected to a Mindsensors NXTServo-v3 Servo module
and tried to add some code to the original NXC library so that I can read out the current position of
a servo.
For testing purpose I just coded a routine routine for port 1 on the controller board.
Here´s the function I added to the library:
It seems that this is working. SOMEHOW......
......because I have to call this function twice each time I want to read the position.......
So, what´s wrong with my function? Any ideas?
I read in the general I2C documentation, that you have to start the reading with a write command.
As I2CBytes seems to be the higher-level wrapper for this You shouldn´t need it.
Or am I missing something?
Any help/hint is wellcom.
Thanks in advance
noob
I have several Hitec Servos connected to a Mindsensors NXTServo-v3 Servo module
and tried to add some code to the original NXC library so that I can read out the current position of
a servo.
For testing purpose I just coded a routine routine for port 1 on the controller board.
Here´s the function I added to the library:
Code: Select all
void ShowServo1Position(byte prt, byte adr, byte reg)
{
SetSensorLowspeed(prt);
int result = -1;
int cnt = 2;
byte outbuf[];
byte cmdbuf[];
byte nByteReady = 0;
string pos;
ArrayBuild(cmdbuf, adr, reg);
while (I2CStatus(prt, nByteReady) == STAT_COMM_PENDING)
{
Wait(10);
}
if(I2CBytes(prt, cmdbuf, cnt, outbuf))
{
result = outbuf[0];
if(cnt==2)
result = result + outbuf[1]*256;
}
pos = NumToStr(result);
TextOut(0, LCD_LINE4, pos, true);
}
......because I have to call this function twice each time I want to read the position.......
Code: Select all
#include "180813_lib.nxc"
#define SensorPort S1
const byte ServoAddr = 0xb0;
task main ()
{
SetSensorLowspeed(SensorPort);
SetServoSpeed(SensorPort, ServoAddr, 1, 100);
while(true)
{
QuickServoSetup(SensorPort, ServoAddr, 1, 50);
Wait (2000);
ShowServo1Position(SensorPort, ServoAddr, 0x42);
ShowServo1Position(SensorPort, ServoAddr, 0x42);
QuickServoSetup(SensorPort, ServoAddr, 1, 200);
Wait (2000);
ShowServo1Position(SensorPort, ServoAddr, 0x42);
ShowServo1Position(SensorPort, ServoAddr, 0x42);
}
}
I read in the general I2C documentation, that you have to start the reading with a write command.
As I2CBytes seems to be the higher-level wrapper for this You shouldn´t need it.
Or am I missing something?
Any help/hint is wellcom.
Thanks in advance
noob