Sensors and Sensing Motors, Encoders and Motor Control Todor Stoyanov Mobile Robotics and Olfaction Lab Center for Applied Autonomous Sensor Systems Örebro University, Sweden todor.stoyanov@oru.se 05.11.2015 T. Stoyanov (MRO Lab, AASS) Sensors & Sensing 05.11.2015 1 / 27
Outline 1 Motors 2 Encoders 3 Motor Control 4 Embedded Control 5 Practice: Arduino T. Stoyanov (MRO Lab, AASS) Sensors & Sensing 05.11.2015 2 / 27
Motors Outline 1 Motors 2 Encoders 3 Motor Control 4 Embedded Control 5 Practice: Arduino T. Stoyanov (MRO Lab, AASS) Sensors & Sensing 05.11.2015 2 / 27
Motors Motor Basics A motor is a device which transforms input energy into kinetic energy. Input energy could be chemical (e.g. gasoline engine), but we are interested in electro-magnetic motors. First electric motors were invented in the 19th century. Hungarian physicist Ányos Jedlik demonstrated the first electric motor. T. Stoyanov (MRO Lab, AASS) Sensors & Sensing 05.11.2015 2 / 27
Motors Motor Basics A motor is a device which transforms input energy into kinetic energy. Input energy could be chemical (e.g. gasoline engine), but we are interested in electro-magnetic motors. First electric motors were invented in the 19th century. Hungarian physicist Ányos Jedlik demonstrated the first electric motor. Figure : Image source: wikipedia T. Stoyanov (MRO Lab, AASS) Sensors & Sensing 05.11.2015 2 / 27
Motors Brush Motors Brush motors were the predominant type of motor until the 1960s. The design includes three key components: the stator, the rotor and the commutator. Stator is typically implemented as a permanent magnet. Rotors are coils which act as magnets when electricity flows through them. The commutator is a mechanical device used for switching the direction of electric current. S + - N T. Stoyanov (MRO Lab, AASS) Sensors & Sensing 05.11.2015 3 / 27
Motors Brush Motors Brush motors were the predominant type of motor until the 1960s. The design includes three key components: the stator, the rotor and the commutator. Stator is typically implemented as a permanent magnet. Rotors are coils which act as magnets when electricity flows through them. The commutator is a mechanical device used for switching the direction of electric current. S + - N T. Stoyanov (MRO Lab, AASS) Sensors & Sensing 05.11.2015 3 / 27
Motors Brush Motors Brush motors were the predominant type of motor until the 1960s. The design includes three key components: the stator, the rotor and the commutator. Stator is typically implemented as a permanent magnet. Rotors are coils which act as magnets when electricity flows through them. The commutator is a mechanical device used for switching the direction of electric current. S + - N T. Stoyanov (MRO Lab, AASS) Sensors & Sensing 05.11.2015 3 / 27
Motors Brushless Motors Brush motors have a major disadvantage: the mechanical components wear off and generate sparks. Digital logic circuits invented in the 60s allowed for brushless commutator designs. Typical brushless motors use a smaller and lighter permanent magnet, suspended in a magnetic field generated by static coils. The commutator is typically implemented using semiconductor elements. switch logic + - T. Stoyanov (MRO Lab, AASS) Sensors & Sensing 05.11.2015 4 / 27
Motors Brushless Motors Brush motors have a major disadvantage: the mechanical components wear off and generate sparks. Digital logic circuits invented in the 60s allowed for brushless commutator designs. Typical brushless motors use a smaller and lighter permanent magnet, suspended in a magnetic field generated by static coils. The commutator is typically implemented using semiconductor elements. switch logic + - T. Stoyanov (MRO Lab, AASS) Sensors & Sensing 05.11.2015 4 / 27
Motors Brushless Motors Brush motors have a major disadvantage: the mechanical components wear off and generate sparks. Digital logic circuits invented in the 60s allowed for brushless commutator designs. Typical brushless motors use a smaller and lighter permanent magnet, suspended in a magnetic field generated by static coils. The commutator is typically implemented using semiconductor elements. switch logic + - T. Stoyanov (MRO Lab, AASS) Sensors & Sensing 05.11.2015 4 / 27
Stepper Motors and Servos Motors Motors in robotics and automation applications often have to be actuated to achieve particular rotational increments. Most popular designs for this application are stepper motors and servos. Stepper motors are brushless motors with a cogwheel-like tooth design. By using a higher number of stators, stepper motors can be commanded to actuate one tooth at a time and can achieve and hold desired number of rotational ticks. Servos are typically implemented as a brushless motor, coupled with a rotary encoder and a microcontroller. We will discuss servos more in the lab. T. Stoyanov (MRO Lab, AASS) Sensors & Sensing 05.11.2015 5 / 27
Stepper Motors and Servos Motors Motors in robotics and automation applications often have to be actuated to achieve particular rotational increments. Most popular designs for this application are stepper motors and servos. Stepper motors are brushless motors with a cogwheel-like tooth design. By using a higher number of stators, stepper motors can be commanded to actuate one tooth at a time and can achieve and hold desired number of rotational ticks. Servos are typically implemented as a brushless motor, coupled with a rotary encoder and a microcontroller. We will discuss servos more in the lab. T. Stoyanov (MRO Lab, AASS) Sensors & Sensing 05.11.2015 5 / 27
Stepper Motors and Servos Motors Motors in robotics and automation applications often have to be actuated to achieve particular rotational increments. Most popular designs for this application are stepper motors and servos. Stepper motors are brushless motors with a cogwheel-like tooth design. By using a higher number of stators, stepper motors can be commanded to actuate one tooth at a time and can achieve and hold desired number of rotational ticks. Servos are typically implemented as a brushless motor, coupled with a rotary encoder and a microcontroller. We will discuss servos more in the lab. T. Stoyanov (MRO Lab, AASS) Sensors & Sensing 05.11.2015 5 / 27
Motors Actuating Motors: Pulse Width Modulation DC motors are actuated with a fixed input voltage. In order to move slower or faster, we could change the set input voltage (between 0 and V max ) In digital circuits it is typically easier to use pulse width modulation to achieve control values. Wider pulses integrate to larger area under the curve and longer duty cycles. This is also safer for the motor. 30% 50% T. Stoyanov (MRO Lab, AASS) Sensors & Sensing 05.11.2015 6 / 27
Motors Torque, RPM and Gears In some applications the motor output is directly linked to the driven mechanical system (direct drive). However, it is also common to drive output through a system of gears. Given two gears A and B with radius r a and r b, the contact point moves at the same tangetial speed: v = r a ω a = r b ω b The number of teeth on each gear n a and n b is proportional to the circomference, and thus to the radius of the gear. Therefore: ω a = n b ω b n a Conversely, the exertable torques τ a and τ b are proportional to the arm lengths r a,r b, and thus: τ b = n b τ a n a T. Stoyanov (MRO Lab, AASS) Sensors & Sensing 05.11.2015 7 / 27
Motors Torque, RPM and Gears A high input to output gear ratio (n a > n b ) reduces the exerted torque and increases the angular velocity. More commonly, gear trains with a higher number of output teeth are used and the gear ratio is reported as n b : 1. Gear systems are characterized also by: efficiency: the ratio of useful torque transmitted (some torque is always lost due to friction and inertia) backclash: how much the output shaft can be rotated back before the input shaft moves backdrivability: can rotations on the output shaft drive back the motor. T. Stoyanov (MRO Lab, AASS) Sensors & Sensing 05.11.2015 7 / 27
Motors Motor Data Sheets Parameters of motors to check when designing your application: Operating voltage Maximum load current Maximum short-term current Maximum torque Maximum rpm Gear ratio T. Stoyanov (MRO Lab, AASS) Sensors & Sensing 05.11.2015 8 / 27
Encoders Outline 1 Motors 2 Encoders 3 Motor Control 4 Embedded Control 5 Practice: Arduino T. Stoyanov (MRO Lab, AASS) Sensors & Sensing 05.11.2015 9 / 27
Encoders Encoder Basics The rotary encoder is one of the basic sensors in robotics A rotary encoder is typically mounted on a motor shaft or gear output shaft and used to count the numbers (and fractions) of rotation. Encoder output is typically used as feedback for motor control in servo systems. Wheel encoder readings are used in vehicle kinematic models to deduce the relative position and orientation of mobile robots (odometry). T. Stoyanov (MRO Lab, AASS) Sensors & Sensing 05.11.2015 9 / 27
Encoders Potentiometers A potentiometer consists of a resistor and a movable contact element. When the contact element moves, it divides the resistor in to two resistance elements R 1 and R 2. The terminals are connected over a load resistor R L and the voltage over R L is calculated as V L = R 1 R 1 +R 2 V in This is a typical voltage divider circuit. Knowing V in and R 1 + R 2, we can deduce the position of the contact element from the measured voltage V L. T. Stoyanov (MRO Lab, AASS) Sensors & Sensing 05.11.2015 10 / 27
Encoders Optical Encoders: Incremental Potentiometers don t allow for continuous rotation. Optical encoders can provide continuous rotation measurements using light sensitive elements and an encoded disk. T. Stoyanov (MRO Lab, AASS) Sensors & Sensing 05.11.2015 11 / 27
Encoders Optical Encoders: Incremental Incremental optical encoders use one disk with equally spaced black lines to generate multiple signal pulses. A second disk is added at a 90 o phase offset to add for direction variance. light source detector light source detector T. Stoyanov (MRO Lab, AASS) Sensors & Sensing 05.11.2015 11 / 27
Encoders Optical Encoders: Incremental Incremental optical encoders use one disk with equally spaced black lines to generate multiple signal pulses. A second disk is added at a 90 o phase offset to add for direction variance. light source detector light source detector A B T. Stoyanov (MRO Lab, AASS) Sensors & Sensing 05.11.2015 11 / 27
Encoders Optical Encoders: Incremental Incremental optical encoders use one disk with equally spaced black lines to generate multiple signal pulses. A second disk is added at a 90 o phase offset to add for direction variance. light source detector light source detector A B T. Stoyanov (MRO Lab, AASS) Sensors & Sensing 05.11.2015 11 / 27
Encoders Optical Encoders: Absolute Absolute encoders solve the problem of initialization errors in incremental encoders. Each tick of the encoder has a unique optical bit word. With gray codes neighboring words have only 1 different bit, adding robustness to measurement error. 10100000 T. Stoyanov (MRO Lab, AASS) Sensors & Sensing 05.11.2015 12 / 27
Encoders Optical Encoders: Absolute Absolute encoders solve the problem of initialization errors in incremental encoders. Each tick of the encoder has a unique optical bit word. With gray codes neighboring words have only 1 different bit, adding robustness to measurement error. 01100000 10100000 T. Stoyanov (MRO Lab, AASS) Sensors & Sensing 05.11.2015 12 / 27
Encoders Optical Encoders: Absolute Absolute encoders solve the problem of initialization errors in incremental encoders. Each tick of the encoder has a unique optical bit word. With gray codes neighboring words have only 1 different bit, adding robustness to measurement error. T. Stoyanov (MRO Lab, AASS) Sensors & Sensing 05.11.2015 12 / 27
Encoders Magnetic Encoders Sensors based on magnetic field strength can also be used to implement rotary encoders. Typical magnetic encoders use the Hall effect which produces voltage potential proportional to the proximity of a magnet. A small disc permanent magnet is fixed on the rotating shaft, while four hall sensors provide 360 degree sensitivity. + - T. Stoyanov (MRO Lab, AASS) Sensors & Sensing 05.11.2015 13 / 27
Encoders Magnetic Encoders Sensors based on magnetic field strength can also be used to implement rotary encoders. Typical magnetic encoders use the Hall effect which produces voltage potential proportional to the proximity of a magnet. A small disc permanent magnet is fixed on the rotating shaft, while four hall sensors provide 360 degree sensitivity. + + - + - + + - + + - - - Hall voltage + - T. Stoyanov (MRO Lab, AASS) Sensors & Sensing 05.11.2015 13 / 27
Encoders Magnetic Encoders Sensors based on magnetic field strength can also be used to implement rotary encoders. Typical magnetic encoders use the Hall effect which produces voltage potential proportional to the proximity of a magnet. A small disc permanent magnet is fixed on the rotating shaft, while four hall sensors provide 360 degree sensitivity. T. Stoyanov (MRO Lab, AASS) Sensors & Sensing 05.11.2015 13 / 27
Encoders Interfacing Encoders Encoders are often packaged with their own microcontrollers for low-level logic. Reading encoder measurements from those is device-dependent and follows manufacturer-defined protocols. Occasionally, you may need to read encoder ticks directly in your microcontroller code. The encoder square waves are often tied to interrupts on the microprocessor. Once an interrupt is triggered, you may need to check other input pins to determine direction of the wave, before updating encoder positions incrementally. You will get some practical experience with this in the lab. T. Stoyanov (MRO Lab, AASS) Sensors & Sensing 05.11.2015 14 / 27
Motor Control Outline 1 Motors 2 Encoders 3 Motor Control 4 Embedded Control 5 Practice: Arduino T. Stoyanov (MRO Lab, AASS) Sensors & Sensing 05.11.2015 15 / 27
Motor Control Open-loop Motor Actuation Open loop control is generally a very bad idea in precision applications It may however make sense if you just need to move for a pre-defined time at a pre-defined rate: just set the motor duty cycle to a specific value and hope for the best Motor control however implies you need to have some more sophisticated feedback: usually in terms of encoder ticks, but also load current, force or torque sensors. The most common control framework for simple motor control is the PID controller. T. Stoyanov (MRO Lab, AASS) Sensors & Sensing 05.11.2015 15 / 27
control action measured signal Motor Control PID Control The Proportional Integral Derivative (PID) control framework scales control outputs in accordance with the error, it s derivative and integral. Motor model predicted setpoint error Given a function, which generates set point values r(t), the objective of the controller is to produce control actions u(t), such that the error e(t) = y(t) r(t) is minimized. T. Stoyanov (MRO Lab, AASS) Sensors & Sensing 05.11.2015 16 / 27
Motor Control PID Control Proportional term: The proportional term applies a control, directly proportional to the current error e(t) The control action is then u(t) = K p e(t) Pure P-control suffers from overshoots and oscillations around the steady state, as well Amplitude 1.4 1.2 1 0.8 0.6 0.4 0.2 Step Response Kp=100 Kp=200 Kp=400 0 as steady state errors. 0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2 Time (seconds) T. Stoyanov (MRO Lab, AASS) Sensors & Sensing 05.11.2015 16 / 27
Motor Control PID Control Derivative term: The derivative term applies controls, proportional to the derivative of the error. The derivative control action is then u(t) = K d d dt e(t). Derivative terms dampen oscillations and act to smoothen the system behavior. Typically, used in combination with a P-term. Amplitude 1.4 1.2 1 0.8 0.6 0.4 0.2 Step Response Kd=10 Kd=20 Kd=40 0 0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2 Time (seconds) T. Stoyanov (MRO Lab, AASS) Sensors & Sensing 05.11.2015 16 / 27
Motor Control PID Control Integral term: The integral term applies a control, proportional to the sum of all previous errors. The integral control action is then u(t) = K i t 0 e(t)dt. Integral terms decrease the steady state error, as they accumulate even small errors over time to generate high enough controls. Typically used in combination with a P-term. Amplitude 1.5 1 0.5 Step Response Ki=100 Ki=200 Ki=400 0 0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2 Time (seconds) T. Stoyanov (MRO Lab, AASS) Sensors & Sensing 05.11.2015 16 / 27
Motor Control PID Control Step Response 1 Kp=400, Ki=500, Kd=40 0.9 0.8 0.7 Amplitude 0.6 0.5 0.4 0.3 0.2 0.1 0 0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2 Time (seconds) The full PID equation is then: u(t) = K p e(t) + K i t 0 d e(t)dt + K d e(t) (1) dt T. Stoyanov (MRO Lab, AASS) Sensors & Sensing 05.11.2015 16 / 27
Motor Control Tuning of PID Gains Parameter Rise time Overshoot Settling time Steady-state error K p Decrease Increase Small change Decrease K i Decrease Increase Increase Eliminate K d Small change Decrease Decrease No effect Start by tuning K p. Increase until you see steady state oscillations. Add a K d term to decrease overshoot. If there is a steady state error, add a small K i to eliminate it. T. Stoyanov (MRO Lab, AASS) Sensors & Sensing 05.11.2015 17 / 27
Motor Control Common Modes of Motor Control Position control: use an encoder after the gears to feedback the position for control. Velocity control: use the first time derivative of the encoder position as a control variable. Current control: use the electrical current absorption for control. Force/Torque control: couple the motor with a force or torque sensor and use the output in control space. Common mode for achieving compliance. T. Stoyanov (MRO Lab, AASS) Sensors & Sensing 05.11.2015 18 / 27
Motor Control Generating smooth motion It is often desireable to generate a smooth trajectory for the driven system (manipulator or wheeled base). Given the position of a motor x(t) (offset from a reference angle in radians) Non-smooth x(t) may require an infinite velocity x (t) = d dt x(t) Similarly, the velocity needs to be smooth, or else we risk infinite acceleration x (t) Ideally, we want a smoothly varying acceleration profile, thus we want to minimize the integral of the jerk x (t) over the motion. Theoretically[1], this occurs when the sixth derivative of x(t) vanishes. T. Stoyanov (MRO Lab, AASS) Sensors & Sensing 05.11.2015 19 / 27
Motor Control Generating smooth motion We represent x(t) as a fifth order polynomial: With derivatives: x(t) = a 0 + a 1 t + a 2 t 2 + a 3 t 3 + a 4 t 4 + a 5 t 5 (2) x (t) = a 1 + 2a 2 t + 3a 3 t 2 + 4a 4 t 3 + 5a 5 t 4 (3) x (t) = 2a 2 + 6a 3 t + 12a 4 t 2 + 20a 5 t 3 (4) Assuming we start at rest at 0 and want to finish at rest at d in time τ, we know the boundary conditions: x(0) = 0, x(τ) = d (5) x (0) = 0, x (τ) = 0 (6) x (0) = 0, x (τ) = 0 (7) T. Stoyanov (MRO Lab, AASS) Sensors & Sensing 05.11.2015 20 / 27
Motor Control Generating smooth motion This leaves us with 6 equations for 6 unknowns, with the solution: ( ( t ) 3 ( t ) 4 ( t ) 5 ) x(t) = d 10 15 + 6 τ τ τ (8) Why minimum jerk? -> keeps the maximum velocity and acceleration within reasonable bounds. In practice, reduces the size of the step sent to the PID controllers. T. Stoyanov (MRO Lab, AASS) Sensors & Sensing 05.11.2015 21 / 27
Embedded Control Outline 1 Motors 2 Encoders 3 Motor Control 4 Embedded Control 5 Practice: Arduino T. Stoyanov (MRO Lab, AASS) Sensors & Sensing 05.11.2015 22 / 27
Embedded Control Microcontrollers Control of actuators is usually implemented on dedicated embedded devices. A microcontroller is essentially a single integrated circuit that provides a processor core, a memory array and input/output operations. Microcontrolers packaged on a printed circuit board with additional ICs and dedicated programming environments are often used used for convenience. Figure : Arduino Due microcontroller board T. Stoyanov (MRO Lab, AASS) Sensors & Sensing 05.11.2015 22 / 27
Embedded Control Digital I/O Microcontroller boards like the Arduino have a number of PINs that can be configured for input or output operations Pins for digital I/O can read/write logic values - high or low High values represent a fixed voltage potential relative to the digital ground Low values have zero potential relative to ground When using digital I/O the logic ground for target devices has to be set to the ground terminal of the board. Example applications: reading in binary streams from sensors, detecting switches T. Stoyanov (MRO Lab, AASS) Sensors & Sensing 05.11.2015 23 / 27
Embedded Control Analogue I/O Analogue input pins can be configured to measure voltage potentials relative to ground level. Analogue output pins produce a pulse-width modulated (PWM) square wave signal. Depending on the board, the PWM duty cycle and/or the frequency can be modified. Example applications: measuring values from a current sensor, driving a motor. 30% 50% T. Stoyanov (MRO Lab, AASS) Sensors & Sensing 05.11.2015 24 / 27
Embedded Control Programming Tips Programming for embedded devices can be tricky. Code needs to be cross-compiled for the controller and then uploaded to the on-board flash memory. Debugging can be difficult as outputs come on the serial port and programs cannot be paused. A useful tool for real-time processing are interrupts, which can trigger code pieces when the states of I/O pins change. T. Stoyanov (MRO Lab, AASS) Sensors & Sensing 05.11.2015 25 / 27
Practice: Arduino Outline 1 Motors 2 Encoders 3 Motor Control 4 Embedded Control 5 Practice: Arduino T. Stoyanov (MRO Lab, AASS) Sensors & Sensing 05.11.2015 26 / 27
Practice: Arduino Sample questions True/False questions: a: An array of four hall effect sensors can be used to implement a magnetic shaft encoder. b: Increasing the D term of a PID controller sharply reduces the rise time. c: Incremental encoders can be coupled with a control algorithm and a motor to create a servo motor drive. d: Increasing the I term of a PID controller can lead to steady state oscillations. e: Absolute optical encoders suffer from the initialization errors effect. T. Stoyanov (MRO Lab, AASS) Sensors & Sensing 05.11.2015 26 / 27
Practice: Arduino Sample questions Derive / Design a: Draw a diagram to explain how PID control works. Include the equations for computing the control u based on the PID gains and the error e. (3 points) b: You are designing a velocity controller for a motor with an encoder. Given a setpoint velocity v, how would a PID controller look like? Provide equations for computing the control u. (Hint: you need to use the cycle time dt between updates to compute the current velocity) (4 points) c: Given a k p = 5.5, k i = 0 and k d = 2. Your previous measured encoder positions were q t 2 = 310 and q t 1 = 350 ticks, your current position is q t = 380 and your cycle time d t = 0.01 seconds. If your set point velocity is v = 2000 ticks per second, what would be the output u of your controller? T. Stoyanov (MRO Lab, AASS) Sensors & Sensing 05.11.2015 26 / 27
Practice Session Task Practice: Arduino For this task we will do some basic Arduino programming. Setup the arduino environment and try out the Blink sketch. You can follow the instructions from the Lab exercise for this part. Download the data sheet for the ELS-505GWA display. Your task is to make a countdown timer, which displays numbers from 9 down to 0, changing every second. T. Stoyanov (MRO Lab, AASS) Sensors & Sensing 05.11.2015 27 / 27
Practice: Arduino References [1] Neville Hogan. An organizing principle for a class of voluntary movements. The Journal of Neuroscience, 4(11):2745 2754, 1984. T. Stoyanov (MRO Lab, AASS) Sensors & Sensing 05.11.2015 27 / 27
Practice: Arduino Sensors and Sensing Motors, Encoders and Motor Control Todor Stoyanov Mobile Robotics and Olfaction Lab Center for Applied Autonomous Sensor Systems Örebro University, Sweden todor.stoyanov@oru.se 05.11.2015 T. Stoyanov (MRO Lab, AASS) Sensors & Sensing 05.11.2015 27 / 27