How to read the encoder status (nexus robot 04)

I recently purchased one robot kit from nexus. It has a demo code to move robot like this:

youtube.com/watch?feature=p … 0giZE5tV4k

But I would like to read the four encoder (enbeded in motor)status to calculate how much distance the robot move. Does any know how to get encoder status? Thank you very much. (The attachment is the demo arduino code.)

Ian
10009.zip (2.56 MB)

Hi Ian,

You should be able to read the motor speed (from the encoder) using the getSpeedRPM() and getSpeedPPS() functions. If you want to total pulse count, there’s also a getCurrPulse() function. All these functions are described in more detail in the PDF manual included within that ZIP file.

Hope this helps,

Dear Jarcand,

Thank you very much for your reply.

I changed this program code like this:

[code]int SpeedRPM;

void setup() {
//TCCR0B=TCCR0B&0xf8|0x01; // warning!! it will change millis()
TCCR1B=TCCR1B&0xf8|0x01; // Pin9,Pin10 PWM 31250Hz
TCCR2B=TCCR2B&0xf8|0x01; // Pin3,Pin11 PWM 31250Hz

Omni.PIDEnable(0.31,0.01,0,10);
Serial.begin(9600);
}

void loop() {
Omni.demoActions(200,5000,500,false);
SpeedRPM=Motor.getCurrPulse();
Serial.println(SpeedRPM);
}
[/code]

But it showed this error:** RB009_RB008_Omni4WD_PID:68: error: expected primary-expression before ‘.’ token**

How do I solved this ?

Thank you very much.

“Motor” isn’t a variable in the code, but more a reference to an object of type Motor. Try using wheel1, wheel2, wheel3, or wheel4 instead, which are of type MotorWheel and should work.

Thank you so much ! I changed the code “SpeedRPM=Motor.getCurrPulse()” to “SpeedRPM=wheel1.getCurrPulse();” , and I can read the pulse.

Now I have another quenstion. I would like to change “Omni.demoActions(200,5000,500,false);” this motion. I just want to set theCar Advance, back, left, right , and stop. And I change “Omni.demoActions(200,5000,500,false);” to “Omni.setCarAdvance(200);” (200 is velocity) but it didn’t move forward.Do I make any mistakes?

Thank you very much.

Ian

We’re not very familiar with this part of the code, but by examining the code for the demoActions function, it seems like you might need to do something like this instead:

setCarAdvance(0);
setCarSpeedMMPS(200, 500);
delayMS(5000, false);
setCarSlow2Stop(500);

Does this work?

Yes! I changed to

Omni.setCarAdvance(0); Omni.setCarSpeedMMPS(200, 500); Omni.delayMS(5000, false); Omni.setCarSlow2Stop(500);

and it worked !
setCarSpeedMMPS and delayMS functions are needed.

Thank you very much!

I changed the program like this: But when I input ‘q’ to let car stop. but it only stop a while and then start to run… I don’t know why…

[code]void setup() {
TCCR1B=TCCR1B&0xf8|0x01; // Pin9,Pin10 PWM 31250Hz
TCCR2B=TCCR2B&0xf8|0x01; // Pin3,Pin11 PWM 31250Hz
Omni.PIDEnable(0.31,0.01,0,10);
Serial.begin(9600);
}

void loop() {
int v=200;

char val = Serial.read();
switch(val){

case ‘w’: Car(v,0); break;
case ‘a’: Car(v,2); break;
case ‘d’: Car(v,3); break;
case ‘s’: Car(v,1); break;
case ‘q’: Carstop(); break;
}
}

void Car(int v,int m){
switch(m){
case 0 : Omni.setCarAdvance(0); break;
case 1 : Omni.setCarBackoff(0); break;
case 2 : Omni.setCarLeft(0); break;
case 3 : Omni.setCarRight(0); break;
}
Omni.setCarSpeedMMPS(v, 500);
Omni.delayMS(1000, false);
}

void Carstop(){
int duration=500;
int uptime=500;
Omni.setCarSlow2Stop(uptime) ;
Omni.delayMS(duration,false);
Omni.setCarStop(uptime) ;
Omni.delayMS(duration,false);
}[/code]

It seems the motor question. If I used this code:

[code]Omni.setCarAdvance(0);
Omni.setCarSpeedMMPS(200, 500);
Omni.delayMS(1000, false);

and then used this:

Omni.setCarAdvance(0);
Omni.setCarSpeedMMPS(0, 500);
Omni.delayMS(1000, false);
[/code]
to change the velocity=0 and to stop motor.

the motor velocity will slow down but then start to run again…

Based on the sample code, I think you should be able to use this for your Carstop() function:

void Carstop(){
 int uptime=500;
 Omni.setCarSlow2Stop(uptime) ;
 Omni.setCarStop() ;
}

Finaly I only can stop the PWM ouput,like this:

analogWrite(3,0); analogWrite(11,0); analogWrite(9,0); analogWrite(10,0);

Thank you very much

excuse me, i have a task to acces the sonar sensor in nexus robot. i use the library SONAR.h and try to change the code but the data can read by the serial monitor. if you have any experience in this case. please share to me. or can send email to [email protected]