Motion Control using DC Motors

Contents

Introduction to Motors

For this project, we use two 47:1 Metal Gearmotors. Each gearmotor consists of a high-power, 6 volt brushed DC motor combined with a 46.85:1 metal spur gearbox, and it has an integrated 48 CPR quadrature encoder on the motor shaft, which provides 2248.86 counts per revolution of the gearbox’s output shaft.

Motor Calibration

The requirements of our robot, directly dependent on our motors are as follows:

  1. The robot must be able to move a specified distance in a straight line accurately.
  2. The robot must be able to rotate a specified angle to the left and right accurately.
  3. The robot must be able to calibrate against the front wall
  4. The robot must be able to calibrate against the right wall

Straight Line Motion

For our robot, the left wheel motor runs at a higher rotations per minute (RPM) than the right wheel motor. The RPM of each motor also fluctuates my a small margin, which impacts the straight line motion of the robot.

  • The speed of the right wheel is set higher than that of the left wheel to compensate for the difference in power, allowing the RPM of both motors to be as close as possible.
  • A Proportional-Integral-Derivative (PID) controller was added, creating a closed loop control system. The RPM is recorded by each motor’s encoders, and the difference is sent back to the controller as a feedback, which adjusts the speed of the motors accordingly. This forms a feedback loop that runs continuously ensuring that the RPM of both motors remain as similar as possible.
  • For adjustment of PID values, we employed the Ziegler-Nichols method. All gains are initially set to 0. The proportional gain is then increased until the system was stable with consistent oscillations at 0.5. The stable state error and overshoot was already in acceptable range at this point, and adjustment of the derivative gain and integral gain was unnecessary.
  • To ensure it only moves a specific distance, the initial encoder values were recorded in variables startLeftEncoderValue and startRightEncoderValue.
  • A target distance, fwd_dist, is calculated using the proportional equation fwd_dist = input_dist * k + initial encoder value where k is a constant. If-else statements is used for having different constants for specific distance ranges.
  • A while loop is used to track the current ticks of the motor and compare it to the fwd_dist. Once the encoder value exceeds the fwd_dist, the loop will break and the forward movement is completed.

Rotation to the Left and Right

For the robot to rotate on the spot, the motors are required to rotate at the same speed for a set amount of time. The time will determine the angle which the robot rotates. The ramp up and stopping of the motor takes time, and hence angle to time taken is not linear.

  • In order for the wheels to rotate at the same speed, we used the same PID close loop control system as the straight line motion, which 1 of the wheels going in the opposite direction.
  • The startLeftEncoderValue and startRightEncoderValue variables were declared to mark the initial encoder values for rotateLeft() and rotateRight() respectively.
  • A target_Tick is calculated using the proportional equation target_Tick = Angle * k + initial encoder value where k is a constant. If-else statements is used for having different constants for specific angle ranges.
  • A while loop is used to track the current ticks of the motor and compare it to the target_Ticks. Once the encoder value exceeds the target_Tick, the loop will break and the rotation is completed.

To decide the values of k for each range range, the robot is set on a 360 degree angle sheet and a trial and error approach was used.

Calibrating to the Front

Despite using the PID controller and the encoder values to ensure accuracy, there still exists fluctuations and minor yet significant errors in straight line motion.

  • The alignFront() and checkDistance() functions were created to combat those issues, which are called when the robot is facing the wall.
  • The alignFront() function straightens the robot based on front wall. It utilizes the left front-facing sensor and the right front-facing sensor. The readings are taken from the 2 sensors and the difference is computed. Based on the result, the robot will rotate right or left at a slower speed.
  • A while loop is created, which will track the sensor readings. Once the difference is less than 0.2cm, alignFront() is completed.
  • The checkDistance() function is called after alignFront, and ensures that the robot is not too close or too far from the wall. It utilizes the 3 front sensors. The readings are taken from the sensors and compared to a pre-defined range for each sensor. If the robot is too far from the wall, it will start moving forward slowly. If the robot is too close to the wall, it will start moving backward slowly.
  • A while loop is created, which will track the sensor readings. Once all 3 sensors are within the specified range, checkDistance() is completed.

Calibrating to the Right

There is a substantial distance between each chance it is able to perform calibration to the front. Any minor errors along the way shifts it out from the center of the 3 by 3 area and cause downstream impacts such as wrong mapping or erogenous sensor readings. Out of all the calibrations, this contributes most to the success of the run as it is called most often.

  • The alignRight() and tooCloseToWall() functions were created to combat those issues, which are called every movement since our robot is right hugging.
  • The alignRight() function straightens the robot based on right wall. It utilizes the front right-facing sensor and the back right-facing sensor. The readings are taken from the 2 sensors and the difference is computed. Based on the result, the robot will rotate right or left at a slower speed.
  • A while loop is created, which will track the sensor readings. Once the difference is less than 0.2cm, alignRight() is completed.
  • The tooCloseToWall() function is called after alignRight(), and ensures that the robot is not too close or too far from the wall. It utilizes the same 2 sensors as before. The readings are taken from the sensors and compared to a pre-defined range for each sensor.
  • If the robot is too far from the wall, it will rotate right 90 degrees, it will start moving forward slowly until it exceeds the fixed distance threshold, then rotate left 90 degrees to face it’s original direction. If the robot is too close to the wall, do the same as before, but moving backwards.

All calibrations are set within conditional statements such that it can only be called when the robot is right next to the wall or obstacle for the required sensors. This ensures the robot does not calibrate at wrong times, which might cause it to be off-centered, slanted, and prone to collision.