Close
0%
0%

Smart Motor Driver for Robotics

This motor driver is designed to be interfaced from a host microcontroller by I2C and be able to control the motor with a PID autonomously.

Public Chat
Similar projects worth following
This is a smart motor driver designed to run a Pololu micro gearmotor, the objective of this board is to be able to implement a PID control over this motors with the corresponding RPM feedback using a hall effect sensor. So it must have his own microcontroller and H bridge to be capable of do the work by itself.

The host microcontroller (or any system that can use I2C) will communicate by I2C in order to give the commands to the smart driver module, this includes speed and direction of the motor. The module will automatically implement the PID to maintain the speed and apply more/less power in the motor to archive that.

Also the control module will be capable of driving the motor for a desired distance and then stop when is reached. This will be ideal in robotics and simplify a lot of hard work making it more precise.

This is a smart motor driver designed to run a Pololu micro gearmotor, the objective of this board is to be able to implement a PID control over this motors with the corresponding RPM feedback using a hall effect sensor. So it must have his own microcontroller and H bridge to be capable of do the work by itself.

The host microcontroller (or any system that can use I2C) will communicate by I2C in order to give the commands to the smart driver module, this includes speed and direction of the motor. The module will automatically implement the PID to maintain the speed and apply more/less power in the motor to archive that.

Also the control module will be capable of driving the motor for a desired distance and then stop when is reached. For doing that you must specify the diameter of your wheel and the gearbox relation.

To sense the speed of the motor we will be using a magnetic encoder disk, it is polarized along the surface of the disk. So the hall effect sensor can detect the changes in the magnetic field of the disk and send the signal to the microcontroller. Then using the timer and interrupts we will calculate the RPM of the motor in second plane, so the microcontroller is free to run a PID control algorithm and handle the communication with the host. 

The main idea is to use it in robots, so by using this modules to control the wheels of the robot it can be possible to make precise movements no matter the surface or the battery charge!!!

Also it protects the gears from stripping due to a sudden acceleration and will protect battery power robots to trip the protection circuit caused by a current rush. 

This is a project under development, so please follow me get updates :) This will be an open source project, so if you have any ideas to improve it you are welcome!

Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

BOM_Digikey.csv

Bill of Material from Digikey, this corresponds for the second revision that uses 12F1840 instead of 12F1822.

ms-excel - 1.05 kB - 06/26/2018 at 01:02

Download

smart_motor_controller.pdf

Schematic of the project

Adobe Portable Document Format - 15.70 kB - 06/01/2018 at 17:29

Preview
Download

  • 1 × PIC 12F1840 Pic microcontroller
  • 1 × DRV8838 H-bridge motor driver
  • 1 × TLE4946-2K Hall effect sensor
  • 1 × SM05B-SRSS-TB(LF)(SN) JST 5 Position and 1mm pitch SMD connector
  • 1 × Magnetic Encoder Disc from Pololu Used to send the magnetic signal to the Hall effect sensor

View all 10 components

  • Auto Stop first test

    Danny FR7 days ago 0 comments

    This is going to be just a quick post, here you can see the auto stop implementation working, for more details about how it works you can check a previous post where I explain it. However a full post will come sooner,  I am stuck right now waiting for components to continue the development. 

  • Bug Fixing the PID

    Danny FR07/11/2018 at 03:07 0 comments

    Today I have discovered something new, a problem that suddenly stops the motor when you change direction abruptly. So I have added a little piece of code to detect when you change direction without stopping the motor first. So now it will automatically detect when a sudden direction change is made an stop the motor using PID before executing the direction change.  Now your gearings will be happy 7u7

  • Project moved to MPLAB

    Danny FR07/09/2018 at 02:05 0 comments

    Today I decided to move from MikroC to MPLAB XC8 because the code is too big for demo version of MikroC. And I want that everyone can modify and adapt the code without needing to pay an expensive compiler license. Also the microcontroller will be now the PIC12F1840 instead of the 12F1822 due to memory space available. 

    Github project is already updated with the new MPLAB!!! Feel free to help me improve the project and make your own version ;)

  • Mathematics behind Auto-Stop feature

    Danny FR07/07/2018 at 23:21 0 comments

    One of the fancy features of this driver is the capability of stop the motor automatically at a desired distance, making easier precision navigation in a robot or maybe other kind of applications. Like a  conveyor belt or an actuator, just let your imagination fly. Applications are limitless, trust me ;)

    Sounds good right? But it will not happen magically, we need to do the math. We need to obtain an equation that allows us to convert that desired distance into how many pulses the motor Hall sensor should give us to achieve the goal and stop the motor just there.

    So let's get started, shall we? Normally robots use wheels and wheels are basically circles with a perimeter, so every time the wheel makes one turn the robot moves an equal distance to this perimeter. This is the key to solve the problem, if we know the perimeter of the wheel and the gear ratio of the motor we can calculate how many turns the motor should do to move the robot certain distance. Said this we have the main formulas involved.

    But this way it doesn't help, we a different approach where the perimeter is the desired distance we want. So we really need this one, so Perimeter will be equal to distance.

    This formula applies just for the wheel. Now to obtain how many turns  the motor should turn we just need to multiply it by the gear ratio. 

    Remember that our Hall sensor will detect 3 pulses (or steps) per each turn of the motor, this is where that constants appears in the play. So we finally have: 

    Where G is the gear ratio of the motor, d is the desired distance we will move and D is the diameter of the wheel. That's it, we enter the distance and we get how many pulses our Hall sensor should detect before it stops the motor.  Easier than you thought, right????

    Just for reference, if we have a 45mm diameter wheel a fully turn will be equal to 141.4mm. This is the distance the robot will move for each wheel turn. With a 150:1 gear ratio motor it means that for every one complete turn of the wheel the motor needs to turn 150 times. If the Hall sensor detects 3 pulses for every motor turn we obtain 450 pulses for every turn of the wheel. So if we divide the diameter by the pulses we obtain the resolution. So our robot will be capable of moving an amazing super precise steps of 0.314mm!!! Cooooool...well in an ideal world, we will make some testing and explain more about the "ideal world" in the next post ;)

  • Fully working PID algorithm

    Danny FR06/26/2018 at 01:55 2 comments

    Finally I come with a fully functional PID algorithm ready to rock :)

    Here you can see an acceleration/breaking ramp test, of course you can adjust the response by tuning the gains (Kp, Kd, Ki) to your own preferences. Slow responses may be useful to avoid stripped gears at sudden movements, also when you have a tall inestable robot that can flip upside down. And when you have a stable robot you can reduce response time to have faster movement transitions. A perfect gain adjust will let you have the best of both worlds. 

    Remember that all the code is available on GitHub, I just used the standard PID equation with some tricks to scale the values to proper ones for backward and forward rotation with same equation. Not so much an interesting thing to discuss ;)

    In the next post I will publish about the auto-stop at defined distance mathematics starting from the wheel diameter and gear ratio.   

  • Having troubles with PID control

    Danny FR06/24/2018 at 04:51 0 comments

    Tuning the PID with the motor running forward was easy but transition to backwards was a completely whole new pain in the ass. It works now both directions flawlessly, however there is still a bit of overshoot only when moving backwards starting at 0 RPM because it runs forward a little bit and then in the right direction while the system compensates the error. 

    In the next entry I will explain how I fix the issue and also how to do the PID control like a pro using only simple and standard equations ;)

  • How it calculates motor speed

    Danny FR06/24/2018 at 04:40 0 comments

    In this log I will explain how the microcontroller calculates the motor speed in RPM for be used later in the PID algorithm (spoiler alert, that would be a future topic) .

    First let's take a look to that part of the code, shall we??? We will be using MikroC

    void interrupt() iv 0x0004 ics ICS_AUTO //interrupts
    {
     if (INTCON.f0 == 1 && IOCAF.f4 == 1) //interrupt on change on RA4 triggered
     {
      INTCON.f3 = 0; //disable on change interrupts
      counter ++;  //increment counter one in one
      IOCAF.f4 = 0; //clear interrupt flags
      INTCON.f3 = 1; //enable on change interrupts
     }
     if(PIR1.f0 == 1) //timer1 interrupt, called every 65.536ms
     {
      INTCON.f3 = 0; //disable on change interrupts
      T1CON.f0 =  0; //stop timer1
      rpm = (counter * 300)/gear;  //calculate rpm  (multiplied 15 interrupts in 1 second, divided 3 encoder interrupts per lap, multiplied by 60 to convert to minutes, divided by gear ratio)
      counter = 0;  //clear counter
      if(LATA.f0 == 0) //if motor is running backwards we just turn value negative
      {
        rpm = rpm *-1;
      }
      INTCON.f3 = 1; //enable on change interrupts
      PIR1.f0 = 0; //clear interrutp flag
      T1CON.f0 =  1; //start timer1
     }
    }

    The first thing you can see is that all process is handled using interrupts, so the microcontroller can do other things and then update once a while. The code is pretty straightforward, we use the pin state CHANGE interrupt to count up every time the hall sensor detects a change in polarity from N to S, easy right?

    Next we use a timer to interrupt every 65.5 ms  and do the math, we just use the equation:

    where counter is the number of times the hall sensor has changed polarity from N to S and we multiply by 300 (we divide by 3 changes from N to S are equal to one turn of motor shaft, then multiply by 15 because is the number of timer interrupts that should happen in 1 second and finally we multiply by 60 to convert to minutes). After that we now how many turns the motor will do in one minute(aka RPM), now we only divide it by the gear ratio attached and we have the RPM in the output shaft of gearbox.

    As you can see is easy to make the RPM measurement in second plane, obviously the value will get updated every 65.5 ms. Not so fast, but consistent enough to run a PID algorithm and also let free time for other process. 

  • Tunning PID

    Danny FR06/17/2018 at 04:19 0 comments

    Now the PID function works perfectly, time to tune kp, kd and ki for this specific motor without load and continue the development.

  • Another test of the PID

    Danny FR06/04/2018 at 18:30 0 comments

    Well now we are ready to load the PID control on the pic microcontroller after a few debugging in arduino. Now it's time for one the most challenging parts, the i2c communication in slave mode

  • Robot without PID motor control

    Danny FR06/04/2018 at 04:46 0 comments

    One of the main problems with robots is that the movements can be erratic when the conditions are not appropriate.  For example, here we have a robot passing through a ramp and we can see that the speed is no consistent and the robot almost flips upside down.


    So by using the smart motor driver we can compensate the speed precisely at real time in this kind of conditions and prevent any unwanted situations that can be catastrophic to our little friend.

    The main objective of this project is to create a solution to this scenarios without sacrificing the precious processing time of the robot main control unit, so we can focus on the robot tasks instead of the robot own integrity.

View all 14 project logs

  • 1
    Gather all the parts

    The first thing you wanna do to build your own is to get all the parts. Please go to github and download the eagle files of the board. You can upload directly the design to oshpark, if you want to use another manufacture service you will need the gerber files. 

    Now purchase all the parts, you can get it on element14 or digikey, but be careful when selecting the size and the case style. Make sure everything is correct and in the right size.

    Optionally order the stencil, it will save you a lot of time and frustration. 

  • 2
    Solder all the parts

    Apply solder paste to the board using the stencil. Now put the smd parts in place, make sure to put it in the right way. Then you can solder it using hot air or a reflow oven.

    Finally solder the motor in place making sure is centered to the shaft hole, then put the encoder disk in place.

  • 3
    Burn the firmware

    Now using a pic microcontroller programmer (like the pickit3) burn the supplied firmware into the board. I used pogo pins to make the connection between the board and the programmer. Remember this project is under development, so I will be changing and updating the firmware until it supports all the functions wanted.

View all 4 instructions

Enjoy this project?

Share

Discussions

Similar Projects

Does this project spark your interest?

Become a member to follow this project and never miss any updates