电机控制代码基于simplefoc库
硬件使用:
- 电机:直流无刷电机2804
- 单片机MCU:ESP32 lolin D32
- 编码器:AS5600
- 驱动板:simplefoc mini
- 导线:杜邦线若干
- 安卓手机,安装蓝牙串口助手app
- 使用平台:arduino ide或者vscode+platform io
以上硬件,除手机外,均可以在M创动工坊采购到
代码如下:
#include <Arduino.h>
#include <Wire.h>
#include <SimpleFOC.h>
#include <iostream>
#include <sstream>
#include "BluetoothSerial.h" //蓝牙库
// SDA 21
// SCL 22
// magnetic sensor instance - I2C
MagneticSensorI2C sensor = MagneticSensorI2C(AS5600_I2C);
BluetoothSerial SerialBT; // 实例化蓝牙
BLDCMotor motor = BLDCMotor(7);
BLDCDriver3PWM driver = BLDCDriver3PWM(32, 33, 25, 12);
float target_angle = 0; // 位置、速度、扭矩值
char model = 'a'; // 模式控制,默认位置模式
float change_value = 0.5; // 变化值
void setup()
{
// initialise magnetic sensor hardware
Wire.setClock(400000);
sensor.init();
// link the motor to the sensor
motor.linkSensor(&sensor);
// power supply voltage [V]
driver.voltage_power_supply = 12;
driver.init();
// link the motor and the driver
motor.linkDriver(&driver);
// choose FOC modulation (optional)
motor.foc_modulation = FOCModulationType::SpaceVectorPWM;
// set motion control loop to be used
motor.controller = MotionControlType::angle;
// velocity PI controller parameters
motor.PID_velocity.P = 0.1;
motor.PID_velocity.I = 0.02;