AVR Motor Controller

Written by Pascal Stang | Updated: Sunday February 05, 2006


The AVR Motor Controller is a dual-axis motor controller and driver system. The controller uses PID motor control, and can calculate and execute trapezoidal-profile motion control. The controller accepts motor commands and quadrature encoder inputs, runs a PID and motion control algorithms, and drives motors at up to 50V and 3A per motor. The controller can operate two motors simulaneously, with seperate or synchronized trajectories.

AVR Motor Controller

Hardware Features:

Software/Firmware Features:

Hardware Connection Information:

Connection reference for AVR Motor Controller
(Encoder1 and Motor1 are associated, same for Encoder/Motor2)

Typical motor connection diagram

Motor Command Information:

These commands can be entered interactively or by script via the controller serial port. To communicate interactively with the controller, use a standard serial terminal program such as HyperTerminal or TeraTerm under windows, or minicom under Linux.

All commands are a single command letter with up to two integer numeric arguments seperated by spaces. Commands are case-sensitive (ie. 'G' is not the same as 'g'). Here is an example command session showing the meaning of each command and various syntax:

cmd>s 1         <-- Select motor1
cmd>p 1000      <-- Set Kp to 1000 for motor1
cmd>i 0         <-- Set Ki to zero for motor1
cmd>d 0         <-- Set Kd to zero for motor1
cmd>g 1000      <-- Drive motor to position 1000 (1000 encoder ticks) using PID
cmd>g 0         <-- Drive motor to position 0 (back to initial position)
cmd>g -1000     <-- Drive motor to position -1000 (same amount, opposite direction)

cmd>s 2         <-- Select motor2
cmd>p 1000      <-- Set Kp to 1000 for motor2
cmd>i 0         <-- Set Ki to zero for motor2
cmd>d 0         <-- Set Kd to zero for motor2
cmd>G 2000 2000 <-- Drive both motor1 and 2 to position 2000 (simultaneous)
cmd>G 0 0       <-- Drive both motors back to zero
cmd>w           <-- Write this configuration to EEPROM

cmd>s 1         <-- Select motor1 again
cmd>t 10000     <-- Drive motor to position 10000 using PID with trapezoidal profile

AVR Motor Controller Commands


General Commands






Displays on-line help for available commands

Select Motor
Select motor to control (1 or 2)
Motor Control Mode

mode=0: Control off (motors disabled)
mode=1: Position control
mode=2: Velocity control (use 'v' command to set velocity)
mode=3: Cross-linked control

Goto Position (PID)
position [tics]
Drives selected motor to requested position under direct PID control.
Goto Position (PID)
pos1 [tics]
pos2 [tics]
Drives both motors simulanteously to requested positions under direct PID control (NOTE: pos1 need not equal pos2).
Goto Position (Prof)
position [tics]
Drives selected motor to requested position under PID+PROFILE control. Uses trapezoidal motion profile.
Goto Position (Prof)
pos1 [tics]
pos2 [tics]
Drives both motors simulanteously to requested positions under PID+PROFILE control. Uses trapezoidal motion profile.
Reset Encoders
pos1 [tics]
pos2 [tics]
Resets the encoder counters. Allows reassigning the current physical position to user-selected encoder values. If no arguments are specified then encoders reset to zero.
Read Motor Current
Returns motor current in mA (if current sensing is connected)

Configuration Print/Read/Write

Config Print
Prints settings and coefficients that are currently active.
Config Write
Stores all current settings and coefficients into EEPROM. The settings are automatically loaded at power-on.
Config Load
Load settings and coefficients from EEPROM. Restores all settings to those last saved with 'w' command.

PID Tuning Assistance

Record Response
Sets currently selected motor for response recording. Response recording records the actual motor trajectory for the first 200points after the next 'g' command.
Upload Response
Returns the recorded motor trajectory data from 'r' command. Each data point is the motor position (encoder value) as it approached the target position requested by the 'g' command.
Step Response Test
Runs automatic motor step-response test.

Trapezoidal-Profile Controller Settings

Set Max Velocity
Sets the maximum velocity to be used under trapezoidal profile control. Also used in velocity control mode to set desired velocity.
Set Max Acceleration
Sets the acceleration to be used under trapezoidal profile control

PID Controller Settings

Set Prop. Gain
Sets the Proportional gain for currently selected motor [units TBD]
Set Integral Gain
Sets the Integral gain for currently selected motor [units TBD]
Set Derivative Gain
Sets the Derivative gain for currently selected motor [units TBD]
Set Windup Max
Sets the maximum value for integral error (clamping value)
Set Output Max
Sets the maximum motor drive level [PWM units, out of 1200]
Set Deadzone
Sets the minimum motor drive level [PWM units, out of 1200]
Below this level, the motor power will be clamped to zero.

System Test Commands

Encoder Test
Prints encoder count value for both motors. Press any key to exit.
Motor Driver Test
Runs motors in ramp test pattern in both directions. Press RESET to exit.
This command list current as of firmware V1.6g, but is subject to change.

PID Control Loop Tuning:

Manual "by feel" Tuning

You can do a reasonable job of tuning a PID controller just by feel and observation. In general, you want to follow these basic steps:

  1. Set Kd and Ki to zero
  2. Pick a low starting Kp
  3. Command the motor to move under PID control (a good amount of motion might be 1 shaft turn)
  4. If the motor moves slugishly, increase Kp and repeat step 3.
  5. If the motor 'runs away' then use negative Kp or switch the polarity of the leads to the motor
  6. Keep increasing Kp until either:
  7. Now begin increasing Kd until the overshoot is damped and/or the oscillation stops.
  8. You now have a basic PID loop tuning.
  9. If needed, you can repeat the tuning process but starting with the values you have now. Return to step 3.
  10. Finally if you have appreciable steady-state loads on the motor, you may want to set a non-zero Ki.
  11. Increase Ki just enough to reduce steady-state error (too much Ki will cause sluggish settling times)

Tuning hints:

Matlab Step-Response Tuning Tool

This Matlab GUI tool allows interactive graphical tuning of the PID controller. The tool requires Matlab V6.0 or later, and the Instrument Control Toolbox (used for serial port access). Here's how to use it:

  1. Download the tool here: step.zip
  2. Unzip 'step.zip' into a temporary directory
  3. Open Matlab and change directory to the location of 'step.m' (from step2)
  4. Connect the AVR Motor Controller to COM1
  5. Run 'step' in matlab. A GUI window should open.
  6. Pick some PID coefficients or use the defaults.
  7. Click the 'STEP' button to run a step-response test.
  8. The motor will execute the test
  9. The desired and actual response of the motor will be ploted.
  10. Examine the response and adjust the PID coefficients
  11. Goto Step 7.
  12. When satisfied with the tuning, you may save the coefficients you chose to EEPROM by clicking 'Store Coeff'.

About Trapezoidal-Profile Control:

Most applications in robotics require motors to make smooth movements from point A to point B. A PID control loop uses position feedback from a shaft encoder to drive a DC motor to precise positions. Unfortunately, the PID algorithm does not give us easy control over exactly how the motor moves from point A to point B. Rather, the PID controller is usually tuned for fastest possible motion while avoiding natural oscillations. So while PID is good at getting a motor to a certain position quickly, to get a smooth movement, we need some additional control.

Trapezoidal profiles can be used to command PID to create the smooth movement we want. A trapezoidal profile is a three-step movement process to go from point A to point B. This three-step process is exmplained and shown graphically below. Within each step, the profile controller constantly updates the desired PID position to make the motor follow the profile. PID still handles the lowest level of control.

The profile controller in the AVR Motor Controller allows the user to choose the acceleration and maximum velocity. When a target position is entered, the profile controller automatically calculates the required profile and executes.

NOTE: Using the profile controller requires a reasonably tuned PID control loop since the profile controller uses PID control to move the motor precisely.

Written by Pascal Stang | Updated: Sunday February 05, 2006