在arduino上使用Modbus協議控制無編碼器減速電機(一)

使用硬件: GA-12減速電機、arduino uno、L298N電機驅動板

使用軟件:modbus poll、python

首先要明白電機驅動板的工作原理:

通道A使能可以通過PWM調節輸出A的電壓,從而調節其轉速。

其邏輯輸入有:

本次使用的庫文件爲<L298N.h>、"Modbus.h"、"ModbusSerial.h"。均可以在GitHub上找到。

arduino代碼爲:

#include <Arduino.h>
#include "Modbus.h"
#include "ModbusSerial.h"
#include <L298N.h>
const int ENA = 6;
const int IN1 = 8;
const int IN2 = 7;
const int IN3 = 2;
const int IN4 = 4;
const int ENB = 3;
int time_delay = 500;
int speed = 0;
L298N driver(ENA,IN1,IN2,IN3,IN4,ENB); 
//ModBus Port information
#define BAUD        9600
#define ID          1
#define TXPIN       -1

//Define the number of registers, inputs and coils to be created
#define NUM_speed 100
#define NUM_time_delay 101

//Modbus Object
ModbusSerial modbus;

void setup()
{
    //Config Modbus Serial (port, speed, rs485 tx pin)
    modbus.config(&Serial, BAUD, TXPIN);
    //Set the Slave ID
    modbus.setSlaveId(ID); 
    modbus.addHreg(NUM_speed);
    modbus.addHreg(NUM_time_delay);
    modbus.Hreg(NUM_speed,1);
    modbus.Hreg(NUM_time_delay,500);
}

void loop()
{
  driver.setup_motors(HIGH,LOW,HIGH,LOW);
driver.drive_motors(modbus.Hreg(NUM_speed));
    //Run the main modbus task
modbus.task();
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章