实验二百二十二:Arduino FOC无刷电机驱动板 兼容Simple FO...
丢失,重发八、INA240电流传感器测试
● 使用Arduino IDE打开资料里面的电流测试程序 ,“angle_control_current_sense_test”,点击“上传”。
位置/角度运动控制示例
*步骤:
*1)配置电机和磁传感器
*2)运行代码
*3)从串行终端设置目标角度(弧度)
运行程序,电机会来回转动,打开串口监视器,可以监测电机的A相、B相的电流以及电流幅值。用手转动电机,电流值会发生改变。
【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
实验二百二十二:Arduino FOC无刷电机驱动板 兼容Simple FOC Shield V2.0.4(艾尔赛)
项目程序之三:INA240电流传感器测试,位置/角度运动控制
实验开源代码
实验二百二十二:Arduino FOC无刷电机驱动板 兼容Simple FO...
实验开源代码/*
【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
实验二百二十二:Arduino FOC无刷电机驱动板 兼容Simple FOC Shield V2.0.4(艾尔赛)
项目程序之三:INA240电流传感器测试,位置/角度运动控制
*/
#include <SimpleFOC.h>
// magnetic sensor instance - SPI
//MagneticSensorSPI sensor = MagneticSensorSPI(AS5147_SPI, 10);
// magnetic sensor instance - MagneticSensorI2C
MagneticSensorI2C sensor = MagneticSensorI2C(AS5600_I2C);
// magnetic sensor instance - analog output
//MagneticSensorAnalog sensor = MagneticSensorAnalog(A1, 14, 1020);
//无刷直流电机和驱动器实例
BLDCMotor motor = BLDCMotor(7);
BLDCDriver3PWM driver = BLDCDriver3PWM(9, 5, 6, 8);
// Stepper motor & driver instance
//StepperMotor motor = StepperMotor(50);
//StepperDriver4PWM driver = StepperDriver4PWM(9, 5, 10, 6,8);
// 电流传感器
// shunt resistor value
// gain value
// 引脚相位A、B(C可选)
InlineCurrentSense current_sense = InlineCurrentSense(0.01, 50.0, A0, _NC, A2);
// 角度设定点变量
float target_angle = 0;
// instantiate the commander
//Commander command = Commander(Serial);
//void doTarget(char* cmd) { command.scalar(&target_angle, cmd); }
void setup() {
// 初始化电流感应
current_sense.init();
// for SimpleFOCShield v2.01/v2.0.2
current_sense.gain_b *= -1;
// 初始化磁传感器硬件
sensor.init();
// 将电机连接到传感器
motor.linkSensor(&sensor);
// 驱动程序配置
// power supply voltage
driver.voltage_power_supply = 24;
driver.init();
//连接电机和驱动器
motor.linkDriver(&driver);
// 选择FOC调制(可选)
motor.foc_modulation = FOCModulationType::SpaceVectorPWM;
// 设置要使用的运动控制回路
motor.controller = MotionControlType::angle;
// 控制器配置
// default parameters in defaults.h
// 速度PI控制器参数
motor.PID_velocity.P = 0.2;
motor.PID_velocity.I = 2;
motor.PID_velocity.D = 0;
//设置给电机的最大电压
motor.voltage_limit = 1;
//速度低通滤波时间常数
//越低过滤越少
motor.LPF_velocity.Tf = 0.01;
// 角度P控制器
motor.P_angle.P = 5;
//位置控制的最大速度
motor.velocity_limit = 20;
// 使用串行监控
Serial.begin(115200);
// comment out if not needed
//motor.useMonitoring(Serial);
// 初始化电机
motor.init();
// 对齐传感器并启动FOC
motor.initFOC();
// 添加目标命令T
//command.add('T', doTarget, "target angle");
Serial.println(F("电机准备就绪!"));
Serial.println(F("使用串行终端设置目标角度:"));
Serial.println("电流感应就绪!");
_delay(1000);
}
void loop() {
// 主要FOC算法功能
// the faster you run this function the better
// Arduino UNO loop~1kHz
// Bluepill loop ~10kHz
motor.loopFOC();
// 运动控制功能
// 速度、位置或电压(在电机控制器中定义)
// this function can be run at much lower frequency than loopFOC() function
// You can also use motor.move() and set the motor.target in the code
motor.move(target_angle);
// function intended to be used with serial plotter to monitor motor variables
// significantly slowing the execution down!!!!
// motor.monitor();
// user communication
// command.run();
PhaseCurrent_s currents = current_sense.getPhaseCurrents();
float current_magnitude = current_sense.getDCCurrent();
Serial.print(currents.a * 1000); // milli Amps
Serial.print("\t");
Serial.print(currents.b * 1000); // milli Amps
Serial.print("\t");
Serial.print(currents.c * 1000); // milli Amps
Serial.print("\t");
Serial.println(current_magnitude * 1000); // milli Amps
_delay(1000);
}
实验二百二十二:Arduino FOC无刷电机驱动板 兼容Simple FO...
【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)实验二百二十二:Arduino FOC无刷电机驱动板 兼容Simple FOC Shield V2.0.4(艾尔赛)
项目程序之四:角度控制电流感应测试(内联电流感应类)
实验开源代码
/*
【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
实验二百二十二:Arduino FOC无刷电机驱动板 兼容Simple FOC Shield V2.0.4(艾尔赛)
项目程序之四:角度控制电流感应测试(内联电流感应类)
*/
#include <SimpleFOC.h>
// current sensor
// shunt resistor value
// gain value
// pins phase A,B, (C optional)
InlineCurrentSense current_sense = InlineCurrentSense(0.01f, 50.0f, A0, A2);
void setup() {
// initialise the current sensing
current_sense.init();
// for SimpleFOCShield v2.01/v2.0.2
current_sense.gain_b *= -1;
Serial.begin(115200);
Serial.println("Current sense ready.");
}
void loop() {
PhaseCurrent_s currents = current_sense.getPhaseCurrents();
float current_magnitude = current_sense.getDCCurrent();
Serial.print(currents.a * 1000); // milli Amps
Serial.print("\t");
Serial.print(currents.b * 1000); // milli Amps
Serial.print("\t");
Serial.print(currents.c * 1000); // milli Amps
Serial.print("\t");
Serial.println(current_magnitude * 1000); // milli Amps
_delay(1000);
}
实验二百二十二:Arduino FOC无刷电机驱动板 兼容Simple FO...
实验串口返回情况串口输出截图
实验二百二十二:Arduino FOC无刷电机驱动板 兼容Simple ...
九、测试开环位置控制实验注意:12V先不要上电,开环控制电机会发热,上电时间过久容易烧坏。最好等到程序编译上传后再上电,上电尽快操作。开环控制的目的主要是建立对电机控制的初步认识,以及验证电机和驱动板功能是否正常,不要停留太久!
【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
实验二百二十二:Arduino FOC无刷电机驱动板 兼容Simple FOC Shield V2.0.4(艾尔赛)
项目程序之四:测试开环位置控制
实验开源代码
/*
【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
实验二百二十二:Arduino FOC无刷电机驱动板 兼容Simple FOC Shield V2.0.4(艾尔赛)
项目程序之四:测试开环位置控制
*/
// Open loop motor control example
#include <SimpleFOC.h>
// BLDC motor & driver instance
// BLDCMotor motor = BLDCMotor(pole pair number);
BLDCMotor motor = BLDCMotor(7);
// BLDCDriver3PWM driver = BLDCDriver3PWM(pwmA, pwmB, pwmC, Enable(optional));
BLDCDriver3PWM driver = BLDCDriver3PWM(9, 5, 6, 8);
// Stepper motor & driver instance
//StepperMotor motor = StepperMotor(50);
//StepperDriver4PWM driver = StepperDriver4PWM(9, 5, 10, 6,8);
//target variable
float target_velocity = 0;
// instantiate the commander
Commander command = Commander(Serial);
void doTarget(char* cmd) {
command.scalar(&target_velocity, cmd);
}
void doLimit(char* cmd) {
command.scalar(&motor.voltage_limit, cmd);
}
void setup() {
// driver config
// power supply voltage
driver.voltage_power_supply = 12;
// limit the maximal dc voltage the driver can set
// as a protection measure for the low-resistance motors
// this value is fixed on startup
driver.voltage_limit = 6;
driver.init();
// link the motor and the driver
motor.linkDriver(&driver);
// limiting motor movements
// limit the voltage to be set to the motor
// start very low for high resistance motors
// current = voltage / resistance, so try to be well under 1Amp
motor.voltage_limit = 2; //
// open loop control config
motor.controller = MotionControlType::velocity_openloop;
// init motor hardware
motor.init();
// add target command T
command.add('T', doTarget, "target velocity");
command.add('L', doLimit, "voltage limit");
Serial.begin(115200);
Serial.println("电机准备就绪!");
Serial.println("请设置目标位置");
_delay(1000);
}
void loop() {
// open loop velocity movement
// using motor.voltage_limit and motor.velocity_limit
// to turn the motor "backwards", just set a negative target_velocity
motor.move(target_velocity);
// user communication
command.run();
_delay(1000);
}
实验二百二十二:Arduino FOC无刷电机驱动板 兼容Simple ...
实验场景图
页:
1
[2]