Robots - Notes - Motor Control

Introduction

Robot Control Program Skeleton

Small DC Motor Control: Part 1, Uncontrolled Speed (open loop) - using PARTS Mark III controller, PARTS Mark III Sensor Board with H-Bridge Kit and JAL language.

These notes will explain how to make two small DC motors rotate both forward and reverse at variable speeds. We will not explain how to precisely control the speed, such as making both motors run at exactly the same speed, but will cover that in Part II. I will start by building up a block diagram for one motor and controller. Then I will build up the code.

The speed of a small DC motor is controlled by varying the current applied to the motor by a square wave.

Picture of Motor and constant power supply and of motor and square wave generator and batteries.

The amplitude of the square wave is determined by the voltage source (4 to 6 AA batteries) and remains constant. If 4 AA batteries are used then the amplitude is approximately 4.8 volts if 6 AA batteries are used then the amplitude is approximately 7.2 volts.

Same picture as above with amplitude pointed out.

The current is varied by varying the amount of time the voltage is present and the amount of time the voltage is off. One complete on off cycle determines the period of the square wave. The number of times the voltage cycles on and off per second determines the frequency.  The ratio of on time to off time is referred to as the duty-cycle. 

Show picture with 75% on duty cycle

Most computers and microcontrollers use voltage up to 5 volts and draw current in the 10's of milliamps. A DC electric motor normally wants voltages that start at 4.5 volts and currents that start in the 100's of milliamps. DC motors quickly range up into the 12 to 24 volts range and can draw several amps. So it's fairly clear that computer electronics cannot directly supply the required voltage. An H-Bridge driver is used to control the higher power that a motor needs using a smaller signal that computer electronics provides. This driver can switch a higher current on and off and can change the direction of the current using a  smaller signal supplied by the microcontroller.

The diagram below shows a logic symbol of a H-Bridge driver. The main input, 1A,  is a logic level one or zero generated by MCU. This low power signal is replicated as a high power output signal at 1Y. The signal at 1Y is suitable a higher current device like a motor.

  1. Measure motor no load current, stall current and max torque.
  2. Vary power to motors using PWM.
    1. set_pwm1_duty(dutycycle_l)
    2. set_pwm2_duty(dutycycle_r)
  3. Change motor direction using polarity.
    1. pwm1_direction(byte in dir) - Left direction
    2. pwm2_direction(byte in dir) - Right direction
    3. 1 = forward, 0 = reverse
  4. Determine number of motor rotations using wheel encoders.
    1. Get_Sectors_L
    2. Get_Sectors_R
    3. Using RB7:RB4 Interrupt-On-Change to trigger count
  5. Determine speed of wheels using wheel encoders.
    1. Using Timer1 as part of a High Resolution Long Duration Timer
      1. tick1:tick0:tickH:tickL
      2. tickL = .4us increments, up to 1024us (.4us * 256)
      3. tickH = 1024us increments, up to 20ms tickH:tickL up to 20ms (50,000 increments)
      4. tick0 = 20ms increments, up to 5.12s (20ms * 256)
      5. tick1 = 5.12s increments, up to 1310.72s, 21m50.72s, (5.12s * 256)
    2. Get_Sector_Time_L
    3. Get_Sector_Time_R
    4. Using RB7:RB4 Interrupt-On-Change to determine sector duration.
    5. Using 4 sector average.

If you have any questions or comments about this page please email me at: debots_replacethis_dinkdaze.org with at sign.