Stopping continuous servo - arduino

Do you know how to stop continuous servo in desired position?
I tried to code like this (which I found on many forums):
myservo.write(10); //moves forward
delay (1000); //waits 1s
myservo.write(90); //stops
My servo indeed moves forward for 1s then stops in the position I wanted then immediately goes back to its one position (the same position every time). I would like it to stay in the desired position. For example, I counted that in 150ms it moves for angle of 60 degrees and there it should stop and wait for next info.
I also tried to use trick to detach servo like this:
myservo.write(10);
delay(120);
myservo.detach();
delay(2000);
It works, but it is not as precise as I want it to be and I can't reach smaller angles than 100 degrees.
I'm using Servo TowerPro MG996R Robot 360 which is continuous servo.
I'm sorry if there is already the answer for my question but I promise I went through countless topics and forums!
Thank you for any advices!!!

Related

How do I turn a Fitec FS90R servo motor 90 degrees clockwise and anti-clockwise using a PIC18F4550

I have tried varying the duty cycle of the servo motor but it only turns 180 degrees clockwise and anti-clockwise.
You need some measurement device to read the angle, if you want to stick with that servo. With the angle you can implement a closed-loop transfer function to approach the angle you want.
Servos as the FS90R are "continuously rotation servos" and the control signals sets the speed (exactly: the driving current, giving the torque) and direction. For example, see this fine explanation.
If you want to control the angle directly by the control signal, you need "the other" type of servo, described earlier in the linked Wikipedia page.

Arduino movement distance with only ultrasonic senzor

Is it possible measure a robot car movement distance only with ultrasonic sensor ??
I working on robot car, in the picture you can see how i placed my ultrasonic sensors(red points) and my question is if i can get width value??
Yes it is possible.
The low cost HC-SR04 sensor, can measure objects from 2cm to 4-5m.
The sensor will send some pulses and measure the time, until the pulses returns. So you can just measure the time (short pulse = near, long pulse = far).
There is just 1 problem with your car:
- it must reflect your pulses. If you don't have any wall or obstacles, you will always detect the maximal pulse with.
If you are inside a room, you can detect the movement. If you are outside or in a big room (more than 8x8m), this is not possible.

Is it necessary to control a servo with pid

So, I need to build a simple gimbal with three servos to control pitch, roll and yaw. I have a 9dof imu which can give me the euler angles in degrees. Can I just connect these angle errors to servo outputs? As in with 1 degree error, the servo should rotate 1 degree, or do I have to use some form of pid control? I have worked with controlling regular dc motors with pid so that the bigger the error, the faster the motor should rotate to compensate. But it's not like I can adjust the speed the servo rotates at.
I recon there would be a problem when the angle error becomes very high in a small amount of time since the servo would take more time to reach the desired position instead of when the error is very small.
I did a similar project. It isn't perfect, but good enough. Cheap servos can't really be precise because of the cheap potentiometer inside and the horrible plastic gears.
There wont be much error overtime because common servos use a potentiometer and not a rotary encoder. Thus a PID is almost impossible.

mpu 6050 accelerometer value gives positive and negative values for one movement?

I am using an arduino uno, a gy-521 mpu 6050 and the Jeff Rowberg library. While the code is perfect the results are less than desirable. I am using the RealAccel option but when I move the accelerometer to the right the data is first correctly positive numbers increasing because of the rightward movement. But once I stop moving the device there are negative values appearing. For the most part there is an equal value for both positive and negative values, meaning if I moved the accelerometer the equivalent of '100' in the positive direction once I stopped moving there would also be multiple other negative values adding up to about a '-100'value. I assume this is because for every action there is an opposite and equal reaction but if not I would like to know why this happens and even if this is there case there must be a way around this, right ? I previously solved this by multiple if statements basically saying if any of the last 7 numbers were positive(because of noise) then the next x number of numbers would be zero but I would like to hear any other ideas or solutions for this problem.Thank you.
If you accelerate an object in one direction, the object will never slow down or stop. It will keep going in that direction forever. The only way to slow down or stop is to have negative acceleration. You're right about the negatives equaling positives in your case - it's because you're starting at velocity zero and ending at velocity zero.
I think you're actually trying to calculate velocity in that direction, which will start at 0, go above 0, and then return to 0. To get that number, sum (accerations * (time difference)) from time=0 to whatever time you need the velocity. This is called a Riemann sum of accelerations.
For further reading, look for 'kinematics in one dimension'.

Moving object from point a to b (in 2D) that can only move forward and rotate itself

So I have a ship, that has thrusters at the bottom and that can only use these to move forward. It can also rotate itself around its center. Its thrusters gives it acceleration, so it doesn't move at a constant velocity. What I want to do is to tell it "move to point B".
I have come up with a solution but it doesn't work very well and it doesn't rotate smoothly, it moves jerkily and it doesn't end up exactly where it should be, so I have to have a big margin of error.
Is this a normal problem, and if so is there a "standard" way of doing it? Is this an easy problem? I want to make it look like the ship is steering itself to that point, using the constraints (thrusters, rotation) the player has. This excludes just lerping it from point A to B. Or does it?
I'd love some help in solving this problem. Positions are stored in vectors, and it's a 2D problem. Just for reference I'm including my solution, which basically is accelerating the ship until and rotating it to point to the point. I think my implementation of this idea is the problem:
Vector diff = vector_sub(to_point, pos);
float angle = vector_getangle(diff);
float current_angle = vector_getangle(dir);
float angle_diff = rightrange(angle) - rightrange(current_angle);
float len = vector_getlength(diff);
// "Margin of error"
float margin = 15.0;
// Adjust direction, only if we're not stopping the next thing we do (len <= margin)
if ( len > margin && fabs(angle_diff) > 2.0 )
{
dir = vector_setangle(dir, current_angle + (angle_diff)*delta*(MY_PI) - MY_PI/2);
}
else if ( len > margin )
{
dir = vector_normalize(diff);
}
// accelerate ship (if needed)
acc.x = acc.y = speed;
acc = vector_setangle(acc, vector_getangle(dir));
if ( len <= margin )
{
// Player is within margin of error
}
If you are not looking for a very general solution that works online, then there is a simple solution. What I mean by online is continuously re-calculating the actions along the complete trajectory.
Assuming the ship is at rest at start, simply rotate it towards your target point (while still at rest). Now, your ship can reach the target by accelerating for t seconds, rotating back while in motion (for 0.5 seconds as per your constraint), and decelerating for another t seconds. If the distance between current point and destination is d, then the equation you need to solve is:
d = 0.5*a*t^2 + 0.5*a*t + 0.5*a*t^2
The first term is distance traveled while accelerating. The second term is distance traveled while rotating (v*t_rot, v=a*t, t_rot=0.5). The final term is the distance traveled while decelerating. Solve the above for t, and you have your trajectory.
If the ship is moving at start, I would first stop it (just rotate in opposite direction of its speed vector, and decelerate until at rest). Now we know how to reach destination.
The problem with offline trajectory calculation is that it is not very accurate. There is a good chance that you will end up in the vicinity of the target, but not exactly on top of it.
Let's make the problem a little more interesting: the ship cannot rotate without acceleration. Let's call this acceleration vector a_r, a vector that is at a certain angle against the ship's direction (somewhat like having a thruster at an angle at the back). Your task now is to rotate the ship and accelerate in such a direction that the speed component perpendicular to the vector connecting the current position to the target is canceled out. Instead of trying to calculate the vectors offline, I would go with an online approach with this.
The easiest thing to do would be to add the following algorithm calculated at every time interval:
Calculate the vector pointing from ship to destination.
Split your current speed vector into two components: towards the destination, and perpendicular to it.
If perpendicular speed is zero, skip 4
Start rotating towards the negative of the perpendicular vector's direction. If already looking away from it (not exact opposite, but just looking away), also fire main thruster.
This will oscillate a bit, I suspect it will also stabilize after a while. I must admit, I don't know how I would make it stop at destination.
And the final approach is to model the ship's dynamics, and try to linearize it. It will be a non-linear system, so the second step will be necessary. Then convert the model to a discrete time system. And finally apply a control rule to make it reach target point. For this, you can change your state-space from position and speed to error in position and (maybe) error in speed, and finally add a regulation control (a control loop that takes the current state, and generates an input such that the state variables will approach zero).
This last one is fairly difficult in the maths compartment, and you'd probably need to study control engineering a bit to do it. However, you'll get much better results than the above simplistic algorithm - which admittedly might not even work. In addition, you can now apply various optimization rules to it: minimize time to reach target, minimize fuel consumption, minimize distance traveled, etc.

Resources