If I am trying to make an autonomous robot and I need to make the robot turn 90 degrees. How would I tell the robot to do this in code?
P.S. I am using the Adafruit Motorshield V2 with DC Motors
If I am trying to make an autonomous robot and I need to make the robot turn 90 degrees. How would I tell the robot to do this in code?
P.S. I am using the Adafruit Motorshield V2 with DC Motors
Any number of ways depending on what sort of robot it is, how you make it turn and what sensors you have on it that will tell you what direction it's pointing in. But turning an exact angle is one of the more difficult tricks for most hobby-style robots.
Steve
I assume the robot is moving freely. It's not attached to a platform and only rotates freely, right?
If the robot is supposed to turn 90 degrees by turning one motor forward and the other motor backward, you need encoder plates for the motors. The encoder plates with attached photo transistors (or something similar) will create pulses, which tell how much the motors are turned. When you are at that point in your development, you can measure what one pulse means. Measure how many pulses per second is needed for the robot to travel 1 m/s. You might need different values for both wheels, if the wheels are slightly different. How many pulses are needed for 10 rotations on spot? How many does that make for 1/4 rotation (90 degrees).
Say that you count 800 pulses when the robot travels 1 m. And you count 800 pulses per second when you command the motor with AnalogWrite() value 1000 on a PWM pin. Changing the load on the robot will most certain lead to a lower speed than 1 m/s, even if you use same 1000 value. The proper way is therefore to have a speed control on the motors, which is based on the pulses from the encoders.
Without the encoder plates and pulse counting, you can only hope that your robot always turns 90 degrees, when commanded with same values and timing (which you have to calibrate first). Say you put one motor on 3/4 forward and the other on 3/4 backward for 0.2 s and your robot seems to turn 90 degrees. A change in load or a change of surface (friction) will have it turn something else than 90 degrees. Encoder plates will lead to more precise turns, though load and friction will still cause inaccuracy.
Johan_Ha:
command the motor with AnalogWrite() value 1000 on a PWM pin.
analogWrite() only goes up to 255.
You're right. I was thinking Microbit, not Arduino.