robocre8
AboutContactSocial
  • robocre8
  • About
  • Contact
  • Social
  • DOCUMENTATIONS
    • MoboBot Documentation
    • EPMC Documentation
    • EIMU Documentation
  • EIMU TUTORIALS
    • How To Calibrate And Setup The EIMU
    • How To Use EIMU With ROS2
    • How To Use EIMU With Arduino
  • EPMC TUTORIALS
    • How To Setup DC Motor PID Speed Control With The EPMC
  • How To Use the EPMC With ROS2
  • How To Use EPMC With Arduino
  • TUTORIALS
    • How To Install ROS2 Humble Desktop On PC (FULL INSTALL)
    • How To Install ROS2 Humble Base On Raspberry Pi 4B (FULL INSTALL)
Powered by GitBook
On this page
  • EPMC Module I2C Connection Overview
  • EPMC Arduino Test Code - PID Motor Control

How To Use EPMC With Arduino

Learn how to test and integrate the Easy PID Motor Controller (EPMC) in your Arduino project

PreviousHow To Use the EPMC With ROS2NextHow To Install ROS2 Humble Desktop On PC (FULL INSTALL)

Last updated 2 months ago

EPMC Module I2C Connection Overview

When using the EPMC with I2C, you'll need to power the EPMC module via the power port (7v to 12v), while the 5V3 pin of the EPMC I2C port will be connected to the Arduino-based microcontroller power for reference during I2C communication.

For example, if it is connected to an ESP32 microcontroller, the 5V3 pin of the EPMC I2C port will be connected to the ESP32 3.3v power (while the EPMC module is powered via the power port (7v to 12v)).

Also, if it is connected to an Arduino UNO, MEGA, or NANO, the 5V3 pin of the EPMC I2C port will be connected to the Arduino microcontroller’s 5V power (while the EPMC module is powered via the power port (7v to 12v)).

Don’t forget to ensure the EPMC module and the Arduino microcontroller are connected to a common ground.

EPMC Arduino Test Code - PID Motor Control

  • Ensure you have your Geared DC motors connected to the EPMC Module and the motor PID gains already set up. If not, follow the instructions here [link to the post]

  • Download the epmc_arduino I2C comm library from here by clicking on the green Code button.

    if you download it, extract it and change the folder name to epmc_arduino

  • Move the downloaded library file - epmc_arduino - to your Arduino library folder

    e.g on linux: ... home/Arduino/libraries/

    e.g on windows: ... Documents/Arduino/libraries/

    (or any where your arduino libraries are stored)

  • restart your ArduinoIDE and copy this example code into your project to test the EPMC Module (feel free to modify it to your taste).

pid_motor_control.ino

/*
 * Basic example code on how to control via I2C your geared DC motor with quadrature
 * encoder which is already connected to the Easy PID Motor Controller module and have already
 * succesfully set up their velocity PID control using the epmc_setup_application
 *
 * The code basically sends a low target velocity (in rad/s), waits for some time and then
 * sends a high target velocity (in rad/s). it also prints out the motors' angular positions
 * (in rad) and angular velocities (in rad/s).
 *
 * you can copy the code and use it in your project as you will.
 */

// Easy PID Motor Control i2c communication library
#include <epmc.h>

int epmcAddress = 1; // set this address to the same address you have during setup via the GUI app
EPMC motor(epmcAddress);

///////// my sepcial delay function ///////////////
void delayMs(int ms)
{
  for (int i = 0; i < ms; i += 1)
  {
    delayMicroseconds(1000);
  }
}
//////////////////////////////////////////////////

bool isSuccessful;
float angPosA, angPosB; // motorA, motorB (in rad)
float angVelA, angVelB; // motorA, motorB (in rad/sec)

float lowTargetVel = -3.142;  // rad/sec
float highTargetVel = 3.142; // rad/sec
bool sendHigh = true;

long prevTime;
long sampleTime = 100; // millisec

long ctrlPrevTime;
long ctrlSampleTime = 5000; // millisec

void setup()
{
  // start i2c communication
  Wire.begin();

  // setup serial communication to print result on serial minitor
  Serial.begin(115200);

  // wait for the driver module to fully setup (5 to 10 sec)
  for (int i = 0; i <= 6; i += 1)
  {
    delayMs(1000);
    Serial.println(i);
  }
  motor.sendTargetVel(0.00, 0.00); // targetA, targetB
  // int cmd_vel_timeout = 2000; // 0 to deactivate.
  // motor.setCmdTimeout(cmd_vel_timeout); // set motor command velocity timeout
  // motor.getCmdTimeout(cmd_vel_timeout); // get the stored command velocity timeout
  // Serial.print("motor command vel timeout in ms: ");
  // Serial.println(cmd_vel_timeout);

  motor.sendTargetVel(lowTargetVel, lowTargetVel); // targetA, targetB
  sendHigh = true;

  prevTime = millis();
  ctrlPrevTime = millis();
}

void loop()
{
  if ((millis() - ctrlPrevTime) >= ctrlSampleTime)
  {
    if (sendHigh)
    {
      motor.sendTargetVel(highTargetVel, highTargetVel); // targetA, targetB
      sendHigh = false;
    }
    else
    {
      motor.sendTargetVel(lowTargetVel, lowTargetVel); // targetA, targetB
      sendHigh = true;
    }
    ctrlPrevTime = millis();
  }

  if ((millis() - prevTime) >= sampleTime)
  {
    /* CODE SHOULD GO IN HERE*/

    motor.getMotorsPos(angPosA, angPosB); // gets angPosA, angPosB
    motor.getMotorsVel(angVelA, angVelB); // gets angVelA, angVelB

    Serial.print(angPosA, 3);
    Serial.print(", ");
    Serial.println(angVelA, 3);

    Serial.print(angPosB, 3);
    Serial.print(", ");
    Serial.println(angVelB, 3);

    Serial.println();

    prevTime = millis();
  }
}
  • watch the motors controlled precisely

  • read output from serial monitor.

  • adjust the code to your taste and use.