Feasibility of GPS Project

I'm trying to work out the programming in my mind for a project I'm nearly ready to start building. I seem to have come a little stuck trying to work out the programming side of things. I'm not even sure if its possible to do what I want to do and was wondering if you guys could help.

The basic idea is...

I have a few motors that are controlled by 4 separate servo connections. For the motors to stay running each of the 4 servos needs to receive a signal every 40ms even if the speed remains constant. I'm told there is little play in the 40ms timing.

I also have a GPS that updates every 200ms.

From the GPS information I need to do a small amount of math and make a few decisions and then use that information to decide on changes in speed to be made in the motors.

Its fine for the changes in speed to be made slower than the GPS updates. I just have no idea how to do the math from the GPS updates, change the motor speeds and make sure each of the 4 servo channels gets a signal every 40ms.

Is it even possible? I can't seem to make it work in my mind.

I'm a bit of a programming noob so I'm sorry if the answer is obvious.

Just for clarification, your "motors" are being controlled by servos? (you're not using servos as your motors?)

KenF:
Just for clarification, your “motors” are being controlled by servos? (you’re not using servos as your motors?)

Sounds to me more like continuous rotation “ex-” servos.

GhostKarate:
For the motors to stay running each of the 4 servos needs to receive a signal every 40ms even if the speed remains constant.

Well the Arduino servo library in fact refreshes every 20ms, which I imagine would be ok since it’s more frequent than the 40 you require? See attached scope shot from recent test I did; 1.5ms is the default pulse width for a servo.attach and coresponds to 90degreees in a “normal” servo and 0 speed in a continuous rotation “ex-” servo.

servo attached.bmp (219 KB)

Yeah the motors are being controlled by servos, or servo signals (not 100% sure). I know the board that I'm using controls them wants servo signals.

I did just find a Servo Driver Module that seems to fix the problem.

I didn't know the servo library would do that. Just to make sure I understand... So once I set a position for a servo the pin will keep on outputting it even if I move on to other code?

GhostKarate: I didn't know the servo library would do that. Just to make sure I understand... So once I set a position for a servo the pin will keep on outputting it even if I move on to other code?

Hmmm, that's a good question. My test didn't actually go off and do other stuff. Let me test that and I will, as the Governator said, be back.

edit.... although that said, since I put my attach in setup(), and even though I had an empty loop(), it must work that way. Even an empty loop() is run, albeit doing nothing. But I'll test it explicitly, stand by.

JimboZA:

GhostKarate: I didn't know the servo library would do that. Just to make sure I understand... So once I set a position for a servo the pin will keep on outputting it even if I move on to other code?

Hmmm, that's a good question. My test didn't actually go off and do other stuff. Let me test that and I will, as the Governator said, be back.

Thanks.

From what I gather, servo positions are controlled by a PWM signal. It is also my understanding that once you set the pulse width from an arduino (with an analog.write). It will continue to send this PWM signal until told to do otherwise.

So if you set a position for a servo, the servo library converts the desired position into a PWM signal. This it then sends (using an analog.write). Once it has been "sent" this PWM will continue until you do something to change it. So there's no need for a refresh.

KenF: From what I gather, servo positions are controlled by a PWM signal. It is also my understanding that once you set the pulse width from an arduino (with an analog.write). It will continue to send this PWM signal until told to do otherwise.

So if you set a position for a servo, the servo library converts the desired position into a PWM signal. This it then sends (using an analog.write). Once it has been "sent" this PWM will continue until you do something to change it. So there's no need for a refresh.

That's what my previous test showed with my scope output above, but my test didn't explicitly check that the pulse was repeated when the sketch was doing other stuff, although an empty loop() must surely count as doing other stuff.

But I'm testing it explicitly as we speak....

Ok, explicitly tested and yep same results.

The code below, prints stuff in loop() with a 10 second delay, and the 'scope trace shows the pulse is still sent every 20ms. I can also feel that the servo is holding position.

/* test to see servo pulse on scope
test1: attach only and then print stuff in loop()
 
 */
#include <Servo.h> 

Servo myservo;


void setup()
{ 
  Serial.begin(9600);
  pinMode(13, OUTPUT);
  digitalWrite(13, LOW);
  Serial.println("led 13 off");
  Serial.println("attaching servo at default pulse, nothing more");
  myservo.attach(9); 
  Serial.println("servo attached, led13 should be back on");
  digitalWrite(13, HIGH);
}

void loop()
{
  Serial.print("Looping and just printing millis ");
  Serial.println(millis());
  delay(10000);
}

Thanks that makes my code real easy :D. I think I’m ready to start buying things.

JimboZA I love the way you just poped off and tested that. You guys are really cool. I might have to consider sticking around.

GhostKarate: JimboZA I love the way you just poped off and tested that. .

The alternative is washing the dishes...., but youy're welcome.

I might have to consider sticking around

You should.

GhostKarate: JimboZA I love the way you just poped off and tested that. You guys are really cool. I might have to consider sticking around.

Aww but I would have tested it too, perhaps even before jimboZA, but don't have access to a scope at the moment :(

But yeah, JimboZA is pretty cool :)

KenF:
From what I gather, servo positions are controlled by a PWM signal.

By the way, on reflection, that’s not actually true. Yes the signal is a pulse of certain width, as you indeed say here:

It is also my understanding that once you set the pulse width from an arduino

… but that doesn’t make it pulse width modulation. PWM is concerned with the ratio of on to off times, not the absolute width of the pulse. Servo control is concerned with the width in absolute terms. The attached pic shows two 50% PWM signals: one is from an Arduino 490Hz PWM pin, the other from a 980Hz pin. Both have a duty cycle of 50%, but in absolute terms one has an on time twice that of the other. edit: new pic with labels

It’s also wrong to say (my emphasis):

you set the pulse width from an arduino (with an analog.write)

… since that’s done implicitly with a servo.write() or explicitly with a servo.writeMicroseconds().

pwm hi and low freq with labels.bmp (219 KB)