I’ve dusted of my Dagu Rover 5 🙂 While I’ve had my Rover 5 for almost 2 years I’ve never really used the encoders. Unfortunately my Rover 5 was delivered with an encoder that didn’t work right. So I decided to have a look at it and try to fix it. It turned out to be an easy fix. I just needed to adjust the potentiometer on the encoder control board. So now I had 4 working encoders 🙂
I’m using the Dagu 4 channel motor controller and a Dagu Red Back Spider controller on my Rover 5. The Rover 5 is fitted with 4 quadrature encoders. The 4 channel motor controller has 4 mixing circuits for the encoders. The mixing circuit takes the 2 inputs from the quadrature encoder and mixes them into a single output. So I needed to connect the 4 outputs of the mixing circuits to my Red Back Spider. The Red Back spider is based on the ATmega1280 micro controller (Arduino Mega) which has 6 external interrupts. I used interrupt 0 (pin D2), interrupt 1 (pin D3), interrupt 4 (pin D19) and interrupt 5 (pin D18). Interrupt 2 and 3 are also the I2C pins, I’m already using them for my digital compass.
I started with the same code as I had written for my other robot and changed it for 4 motors.
Next step is to integrate this code in my “Rover 5 remote control sketch”.
// http://www.bajdi.com // Rover 5: 4 motors / 4 encoders // Encoder speed control // µController = ATmega1280 volatile unsigned long PulseM[4]; // Time between pulses volatile unsigned long TimeM[4]; // Time when the last pulse got in unsigned long lastPulseM[4] = {10000,10000,10000,10000}; // Time since the last pulse const int targetPulse = 8500; // THIS INTEGER CONTROLS THE SPEED OF THE MOTORS (900 fastest - 8500 slowest) const int DirM[4] = { 30,31,32,33}; // direction pins const int PWMpinM[4] = { 4,5,6,7}; // pwm pins int PWMM[4]; // pwm settings #define runEvery(t) for (static typeof(t) _lasttime;(typeof(t))((typeof(t))millis() - _lasttime) > (t);_lasttime += (t)) void setup() { Serial.begin(115200); //Setup motor pins for(int i=0; i<4; i++) { pinMode(DirM[i], OUTPUT); } for(int i=0; i<4; i++) { pinMode(PWMpinM[i], OUTPUT); } // Setup encoder pins attachInterrupt(0,M1encoder,CHANGE); // pin D2 attachInterrupt(1,M2encoder,CHANGE); // pin D3 attachInterrupt(4,M3encoder,CHANGE); // pin D19 attachInterrupt(5,M4encoder,CHANGE); // pin D18 } void loop(void) { for (int i=0; i<4; i+=1) { lastPulseM[i] = micros() - TimeM[i]; if (lastPulseM[i] > targetPulse) { PWMM[i]+=1; if(PWMM[i] > 200) // Max pwm speed = 200 { PWMM[i] = 200; } } if (lastPulseM[i] < targetPulse) { PWMM[i]-=1; if (PWMM[i] < 0) { PWMM[i] = 0; } } digitalWrite(DirM[i], HIGH); analogWrite(PWMpinM[i], PWMM[i]); } // serial printing runEvery(500){ for (int i=0; i<4; i +=1) { Serial.print("PulseM = "); Serial.println(PulseM[i]); } for (int i=0; i<4; i +=1) { Serial.print("PWMM = "); Serial.println(PWMM[i]); } } } void M1encoder() { PulseM[0]=micros()-TimeM[0]; // time between last state change and this state change TimeM[0]=micros(); // update TimeM[0] with time of most recent state change } void M2encoder() { PulseM[1]=micros()-TimeM[1]; TimeM[1]=micros(); } void M3encoder() { PulseM[2]=micros()-TimeM[2]; TimeM[2]=micros(); } void M4encoder() { PulseM[3]=micros()-TimeM[3]; TimeM[3]=micros(); }
Great thanks for sharing,
quick question thought, what’s the name of your digital compass device ? It wouldnt happen to be the HMC6352 ? if yes could you please help me ?
Appreciated!
I have a HMC5883L, a Devantech CMPS10 and a couple of others. But I don’t have a HMC6352…
Thank you for uploading this!
I ran it now in my Rover and it really works great
You have saved me a bunch of time!
Thanks!
You are only changing the PWM value by one if there is a difference between the current and target encoder pulse time. That works but you must know about PID controllers because I saw a PID used for the balance bot.
What you are doing is adjusting the PWM value by 1. Why not by a fraction of the error? and then the I and D values?
Did you decide to use the change by one method because it works best or because it is easy to implement?
I’ve integrated the above code in my “remote control sketch”, you can find it here: http://www.bajdi.com/arduino-sketches/rover-code.ino
I change the pwm value by 2 in that sketch. The above code was more a way of checking if all 4 encoders worked correctly.
You are of course correct that a PID algorithm is a better solution. I have tried using a PID algorithm. The problem is that the resolution of the encoders on the Rover 5 is very low. So they don’t give very accurate results. I haven’t gotten any better results by using the PID algorithm. But when using the tracks this is not really a problem. But I recently fitted mecanum wheels to my Rover 5. And then you need very accurate speed control. I’ll post some stuff about that in the future.
Hello,
congratulations for your project. Very interseting and well documented. I am trying one similar rover myself.
I would like to indicate, if you don’t mind, that I think there is a mistake in the whell speed calculation. Or maybe I don0t understand how it is working exactly.
As I see, the speed calculation is done, in the loop, substracting the last interrupt time to the current time. As the loop and the interrupt are not synchronized, this difference can be any value from 0 to the actual period time. the actgual speed to use shouldn’t be “PulseM”? This is really the period of the encoder change…
Again.. I may be wrong, correct me if I am, but I don’t understand how can your approach work correctly.
Alex
Hmm, have not played with my Rover 5’s in a long while. You could be right …
Hello! I’ve recently purchased a Rover 5 and the control board, but I’m having a lot of trouble getting a good idea of how to connect the encoder motors. I have the 4 motor, 4 encoder model. I get where the motor output plugs in obviously, but I don’t understand the rest of the connections. If the board is held so the encoder connections are on the right, I believe (for each encoder) it’s ground, yellow, white, red, from top to bottom, on the outside four pins, correct? I’ve searched for any pictures showing the connections and contacted Dagu, but I either get responses so far over my head or so vague they aren’t usable. The next question is… how is the connection to the Arduino made? From the inside four pins? I understand the board combines the two inputs (white and yellow) but then, why are there four outputs as well? Quite confused.
If the encoders are connected on the right side of the board, will I still need the connections on the left for pwm, direction, etc?
Thanks for any help you might offer.
Just read the manual: https://sites.google.com/site/daguproducts/home/instruction-manuals 🙂
Thanks. Cause, you know, I’d not seen that. 😐 I’m apparently not bright enough to be able to make the leap from the two page ‘manual’ to a working robot, which is why I asked help from someone who HAS done it. My real shortcoming right now is my lack of schematic skills. Once I’ve got those down, I’m sure things like this very cryptic ‘manual’ will be of help.
I get that folks want you to ‘do your own work’. I do. I’ve asked these questions of the folks at Dagu and Pololu and Sparkfun – all who’ve made the board at one time or another. I’ve been forwarded the manual by all but those at Sparkfun who’ve not bothered to respond at all. I’ve written to numerous folks who’ve created projects based on the Rover and the controller board, all to no joy. I often get the sense that people want me to try and fail and learn from that and again, I get it. Buying a few boards to fry isn’t my idea of a good time, but I do get it.
I was a programmer for years and once the web became a real thing, I often asked for and offered help. Lots of folks willing to do that. From writing COBOL and JCL to VB and automating software loads, I often learned tricks and tips – and passed those along – from others. I rode a Harley for years. The forums were invaluable for learning and getting help with everything from seat choices to which cams to run depending on how you rode. My home theater was, in its time, incredible and very expensive. The HT forum members often knew my name and quoted my reviews of products. They also gave me ideas and help building that system.
But dang, robots seems to be different. Do not know why. Somehow everyone expects you to have gone from complete beginner to knowing the acronyms and circuits and all the rest, all by yer lonesome. OK. I’m not sure if it’s cause so many of the makers out there did it just that way – by themselves – that they don’t respect those who ask before doing or if it’s because so many people just expect to copy someone else’s code and claim they built that. But man, it’s hard to get help as a noob in this hobby. I do get understand, though. Me? I’ll still offer any help I can to those who’re coming up. Would hate to be the one that turned away someone who had asked everywhere and got no support.
But in the meantime, thanks for the suggestion.
Al, If you are new to this I recommend you start by connecting the PWM and direction pins of the motor controller to your Arduino (1 digital out and 1 pwm pin / motor). Make sure the Arduino and motor controller have a common ground. You must also feed the motor controller with 5V. Then write some simple code to move the motors. Once you have that figured out connect the encoders to your Arduino (1 interrupt pin / motor) and write some more advanced code.
can i get your wiring sketch?i need that for my final project.you can send the document to my email, please(dindranovia@gmail.com).thank you
I don’t make wiring diagrams of such things. Make your own drawing if it’s for a school project, you don’t just copy other peoples work!