There are a few ways to control the motors. Here are several methods:
set_motor_power to apply power to the motor on a scale of -100 to 100 % (full reverse to full forward). This is unregulated motor control. This does not use feedback from the encoders, so it cannot be precise in speed or distance.
set_motor_position to set an absolute target position (in degrees) for the motor to run to (and hold). This automatically adjusts power forward or reverse to reach the absolute target position specified. This is absolute, and will typically be accurate to within a few degrees.
set_motor_dps to run at a specific speed (specified in degrees per second). The speed is absolute, as it will automatically increase or decrease slightly to maintain a constant average speed specified. Functionally, this is basically the same as using set_motor_position and continuously moving the target position (updated 100 times per second for smooth action).
- Combine the functionality of some of the above. You can use
set_motor_limits to limit the power (percent) and/or speed (degrees per second). For example, if you want a motor to run to the absolute position of 180 degrees, at no more than 100 degrees per second speed, you can use
set_motor_limits to specify the speed limit (100 dps), and then
set_motor_position to specify the target position (180 degrees). Another example, is if you want to run at 100 dps speed, but never exceed 50% power level, you can use
set_motor_limits to limit the power (50 %), and
set_motor_dps to specify the speed to run (100 dps).
Note that due to tolerance, the surface the robot is driving on (wheels slipping slightly), etc. it’s not possible to have perfect movements, regardless of the motor control. Encoders, as good as they are, are relative in their environment. While they do provide absolute motor position/rotation feedback, they are essentially using dead reckoning in their environment. To get absolute positioning in an environment, an external reference would be required. This could be a compass (using earth’s magnetic field to determine the robot orientation), GPS, or some other tracking/locating mechanism.
So to answer your question about making a robot drive straight for a specified distance, try starting with something like this:
# reset the encoders to 0 for a common starting point
GPG.reset_motor_encoder(GPG.MOTOR_RIGHT + GPG.MOTOR_LEFT)
# set the speed limit to a reasonable value that both motors will be able to achieve
GPG.set_motor_limits(GPG.MOTOR_RIGHT + GPG.MOTOR_LEFT, dps = 200)
# distance in mm to travel
TravelDistance = 150
# calculate the target position (in degrees) based on the distance to travel
TargetPosition = TravelDistance / GPG.WHEEL_CIRCUMFERENCE * 360
# set the target position to run to
GPG.set_motor_position(GPG.MOTOR_RIGHT + GPG.MOTOR_LEFT, TargetPosition)