I’m going to be slightly pedantic here, but I’m hoping you’ll benefit from seeing the thought process exposed. I’m also hoping someone doesn’t swoop in in the next 5 minutes with the answer all gift wrapped for you. Here’s my sort of thought process on something like this:
What exactly are we trying to do? We have a servo and a joystick. We want to move the servo with the joystick. Specifically, we want to control the speed of the servo by the position of the joystick. Alas, the joystick outputs position, and the servo inputs position, not speed.
Mathematically speaking, what’s the difference between the position input the servo has, and the speed input we want it to have? The mathematical definition of speed is change-in-position per unit of time. So, we need to change a command of change-in-position per unit of time into an actual command of position. We know that speed times time is the change in position over that chunk of time. So if we started at 0", and we moved at 1"/sec for 1 sec, then we’d be at 1". If we moved for another sec, we’d be at 1"+1", or 2". If we sped up to 3"/s for the next second, we’d be at 2"+3", or 5". And we could keep on adding the speed times time to our last position to find out where we are now.
The big question, of course, is what on earth this has to do with controlling the servo. The answer is pretty simple. With my ridiculous example just above, we could determine our position based on our last position, our speed, and how long we moved. You can do the same thing to tell your servo where you want it to be.
Your program has a nice sort-of timed loop in it where it waits for data from the driver’s station. This gives us our chunks of time to work with. Your joystick is telling us how fast we’re moving. The only other thing we need is to know where we were the last time through the loop. So here’s what you do:
- Save the position of the servo from one loop to the next. It’s important that this is a floating point number, not an integer.
- Add your joystick position (times some scaling factor) to the old servo position from the last time through the loop.
- Check that this new position command fits inside the bounds of a good servo command. In your case, check if it’s between 0 to 170. If it’s not, make it fit. That is, if the new position is 172, make it equal 170.
- Send this new position command to the servo, and save the command to use is as the OLD position in your next run of the loop.
- Repeat for 2 minutes.
Actual implementation depends on if you’re using Labview or WindRiver. But it’s important that you only use a floating point number. Your loops are going to be running fairly quickly, so you’ll probably be wanting to add small amounts to the position in each loop. If you’re using an integer and try to add 0.25 to your old position in every loop, it will be rounded down and your servo won’t move. You’d be stuck adding 1, 2, 3, etc. every loop, and that’s pretty likely to be too fast to really be useful.
So, for a more detailed explanation, I’d need to know what programming language you’re using. But the basic idea is to just add your joystick’s value to the position of your servo every time you make a loop.