Hi,
Ok I am working on something pretty hacky and I was looking for some advice ;D. First of my coding is not the best so I’d be grateful for some advice.
Let me explain. I have a .net gadgeteer spider connected via an extender to a servo that has been hacked. The servo speed is being controlled by pwm but because it is hacked for continuous rotation I have removed the stop from the potentiometer and connected it so I can control the position.
All is well - I get the position of the servo from the pot and can control speed and stop the servo.
When working together though to control position it goes back and forward a lot before it stops and is quite slow to respond.
Problems seem to be noise from the servo that causes the potentiometer value to spike and change a lot. I have it on a separate supply with common ground and am trying capacitors but not luck. I also tried smoothing it with a running average but it slows it down too much.
This is the main bit of my code and I wondered if anyone had any suggestion for how to control the position and movement to a new position based on moving the servo and stopping when it reaches the desired position.
Any advice much appreciated.
void compass_MeasurementComplete(Compass sender, Compass.MeasurementCompleteEventArgs e)
{
Debug.Print("Compass" + e.Angle);
move((int)e.Angle);
}
void timer_Tick(GT.Timer timer)
{
compass.RequestSingleMeasurement();
}
int lastPosition;
int movingToPosition; // constantly updated
bool movingForward = false;
// move servo to a particular position
void move(int moveToPosition)
{
// return current pot position
int currentPosition = potDegrees();
Debug.Print("current position: " + currentPosition);
// stop with definable threshold value
if (currentPosition > (moveToPosition - stopThreshold) && currentPosition < (moveToPosition + stopThreshold))
{
// moveServo(stopSpeed);
servo.IsActive = false;
Thread.Sleep(300);
Debug.Print("Stopped");
}
else
{
servo.IsActive = true;
// first work out direction to move in
// forward
if (currentPosition < moveToPosition)
{
moveServo(forwardSpeed);
Debug.Print("Forward");
}
// backwards
else if (moveToPosition < currentPosition)
{
moveServo(reverseSpeed);
Debug.Print("Backwards");
}
}
}
// reads the potentiometer and converts this to degrees of rotation
int potDegrees()
{
// read pot value
double currentValue = pot.ReadProportion();
// map to 360 degrees
double degreeValue = map(currentValue, 0, 1, 0, 360);
// smooth spikes
// int finalValue = smooth((int)degreeValue);
int finalValue = ((int)degreeValue);
// convert to int and return
return (finalValue);
}