908浏览
楼主: 驴友花雕

[项目] 【花雕学编程】Arduino动手做(222)---Simple FOC Shield V2.0.4

[复制链接]

驴友花雕  中级技神
 楼主|

发表于 2024-7-5 11:14:27

实验二百二十二: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电流传感器测试,位置/角度运动控制

实验开源代码



回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2024-7-5 11:15:36

实验二百二十二:Arduino FOC无刷电机驱动板 兼容Simple FO...

实验开源代码

  1. /*
  2.   【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
  3.   实验二百二十二:Arduino FOC无刷电机驱动板 兼容Simple FOC Shield V2.0.4(艾尔赛)
  4.   项目程序之三:INA240电流传感器测试,位置/角度运动控制
  5. */
  6. #include <SimpleFOC.h>
  7. // magnetic sensor instance - SPI
  8. //MagneticSensorSPI sensor = MagneticSensorSPI(AS5147_SPI, 10);
  9. // magnetic sensor instance - MagneticSensorI2C
  10. MagneticSensorI2C sensor = MagneticSensorI2C(AS5600_I2C);
  11. // magnetic sensor instance - analog output
  12. //MagneticSensorAnalog sensor = MagneticSensorAnalog(A1, 14, 1020);
  13. //无刷直流电机和驱动器实例
  14. BLDCMotor motor = BLDCMotor(7);
  15. BLDCDriver3PWM driver = BLDCDriver3PWM(9, 5, 6, 8);
  16. // Stepper motor & driver instance
  17. //StepperMotor motor = StepperMotor(50);
  18. //StepperDriver4PWM driver = StepperDriver4PWM(9, 5, 10, 6,  8);
  19. // 电流传感器
  20. // shunt resistor value
  21. // gain value
  22. // 引脚相位A、B(C可选)
  23. InlineCurrentSense current_sense = InlineCurrentSense(0.01, 50.0, A0, _NC, A2);
  24. // 角度设定点变量
  25. float target_angle = 0;
  26. // instantiate the commander
  27. //Commander command = Commander(Serial);
  28. //void doTarget(char* cmd) { command.scalar(&target_angle, cmd); }
  29. void setup() {
  30.   // 初始化电流感应
  31.   current_sense.init();
  32.   // for SimpleFOCShield v2.01/v2.0.2
  33.   current_sense.gain_b *= -1;
  34.   // 初始化磁传感器硬件
  35.   sensor.init();
  36.   // 将电机连接到传感器
  37.   motor.linkSensor(&sensor);
  38.   // 驱动程序配置
  39.   // power supply voltage [V]
  40.   driver.voltage_power_supply = 24;
  41.   driver.init();
  42.   //连接电机和驱动器
  43.   motor.linkDriver(&driver);
  44.   // 选择FOC调制(可选)
  45.   motor.foc_modulation = FOCModulationType::SpaceVectorPWM;
  46.   // 设置要使用的运动控制回路
  47.   motor.controller = MotionControlType::angle;
  48.   // 控制器配置
  49.   // default parameters in defaults.h
  50.   // 速度PI控制器参数
  51.   motor.PID_velocity.P = 0.2;
  52.   motor.PID_velocity.I = 2;
  53.   motor.PID_velocity.D = 0;
  54.   //设置给电机的最大电压
  55.   motor.voltage_limit = 1;
  56.   //速度低通滤波时间常数
  57.   //越低过滤越少
  58.   motor.LPF_velocity.Tf = 0.01;
  59.   // 角度P控制器
  60.   motor.P_angle.P = 5;
  61.   //位置控制的最大速度
  62.   motor.velocity_limit = 20;
  63.   // 使用串行监控
  64.   Serial.begin(115200);
  65.   // comment out if not needed
  66.   //motor.useMonitoring(Serial);
  67.   // 初始化电机
  68.   motor.init();
  69.   // 对齐传感器并启动FOC
  70.   motor.initFOC();
  71.   // 添加目标命令T
  72.   //command.add('T', doTarget, "target angle");
  73.   Serial.println(F("电机准备就绪!"));
  74.   Serial.println(F("使用串行终端设置目标角度:"));
  75.   Serial.println("电流感应就绪!");
  76.   _delay(1000);
  77. }
  78. void loop() {
  79.   // 主要FOC算法功能
  80.   // the faster you run this function the better
  81.   // Arduino UNO loop  ~1kHz
  82.   // Bluepill loop ~10kHz
  83.   motor.loopFOC();
  84.   // 运动控制功能
  85.   // 速度、位置或电压(在电机控制器中定义)
  86.   // this function can be run at much lower frequency than loopFOC() function
  87.   // You can also use motor.move() and set the motor.target in the code
  88.   motor.move(target_angle);
  89.   // function intended to be used with serial plotter to monitor motor variables
  90.   // significantly slowing the execution down!!!!
  91.   // motor.monitor();
  92.   // user communication
  93.   // command.run();
  94.   PhaseCurrent_s currents = current_sense.getPhaseCurrents();
  95.   float current_magnitude = current_sense.getDCCurrent();
  96.   Serial.print(currents.a * 1000); // milli Amps
  97.   Serial.print("\t");
  98.   Serial.print(currents.b * 1000); // milli Amps
  99.   Serial.print("\t");
  100.   Serial.print(currents.c * 1000); // milli Amps
  101.   Serial.print("\t");
  102.   Serial.println(current_magnitude * 1000); // milli Amps
  103.   _delay(1000);
  104. }
复制代码


回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2024-7-5 11:34:39

实验二百二十二:Arduino FOC无刷电机驱动板 兼容Simple FO...

  【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
  实验二百二十二:Arduino FOC无刷电机驱动板 兼容Simple FOC Shield V2.0.4(艾尔赛)
  项目程序之四:角度控制电流感应测试(内联电流感应类)

实验开源代码

  1. /*
  2.   【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
  3.   实验二百二十二:Arduino FOC无刷电机驱动板 兼容Simple FOC Shield V2.0.4(艾尔赛)
  4.   项目程序之四:角度控制电流感应测试(内联电流感应类)
  5. */
  6. #include <SimpleFOC.h>
  7. // current sensor
  8. // shunt resistor value
  9. // gain value
  10. // pins phase A,B, (C optional)
  11. InlineCurrentSense current_sense = InlineCurrentSense(0.01f, 50.0f, A0, A2);
  12. void setup() {
  13.   // initialise the current sensing
  14.   current_sense.init();
  15.   // for SimpleFOCShield v2.01/v2.0.2
  16.   current_sense.gain_b *= -1;
  17.   Serial.begin(115200);
  18.   Serial.println("Current sense ready.");
  19. }
  20. void loop() {
  21.   PhaseCurrent_s currents = current_sense.getPhaseCurrents();
  22.   float current_magnitude = current_sense.getDCCurrent();
  23.   Serial.print(currents.a * 1000); // milli Amps
  24.   Serial.print("\t");
  25.   Serial.print(currents.b * 1000); // milli Amps
  26.   Serial.print("\t");
  27.   Serial.print(currents.c * 1000); // milli Amps
  28.   Serial.print("\t");
  29.   Serial.println(current_magnitude * 1000); // milli Amps
  30.   _delay(1000);
  31. }
复制代码



回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2024-7-5 11:36:13

实验二百二十二:Arduino FOC无刷电机驱动板 兼容Simple FO...

实验串口返回情况
串口输出截图
【花雕学编程】Arduino动手做(222)---Simple FOC Shield V2.0.4图1




回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2024-7-9 09:51:13

实验二百二十二:Arduino FOC无刷电机驱动板 兼容Simple ...

九、测试开环位置控制

实验注意:12V先不要上电,开环控制电机会发热,上电时间过久容易烧坏。最好等到程序编译上传后再上电,上电尽快操作。开环控制的目的主要是建立对电机控制的初步认识,以及验证电机和驱动板功能是否正常,不要停留太久!


【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
  实验二百二十二:Arduino FOC无刷电机驱动板 兼容Simple FOC Shield V2.0.4(艾尔赛)
  项目程序之四:测试开环位置控制
  实验开源代码

  1. /*
  2.   【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
  3.   实验二百二十二:Arduino FOC无刷电机驱动板 兼容Simple FOC Shield V2.0.4(艾尔赛)
  4.   项目程序之四:测试开环位置控制
  5. */
  6. // Open loop motor control example
  7. #include <SimpleFOC.h>
  8. // BLDC motor & driver instance
  9. // BLDCMotor motor = BLDCMotor(pole pair number);
  10. BLDCMotor motor = BLDCMotor(7);
  11. // BLDCDriver3PWM driver = BLDCDriver3PWM(pwmA, pwmB, pwmC, Enable(optional));
  12. BLDCDriver3PWM driver = BLDCDriver3PWM(9, 5, 6, 8);
  13. // Stepper motor & driver instance
  14. //StepperMotor motor = StepperMotor(50);
  15. //StepperDriver4PWM driver = StepperDriver4PWM(9, 5, 10, 6,  8);
  16. //target variable
  17. float target_velocity = 0;
  18. // instantiate the commander
  19. Commander command = Commander(Serial);
  20. void doTarget(char* cmd) {
  21.   command.scalar(&target_velocity, cmd);
  22. }
  23. void doLimit(char* cmd) {
  24.   command.scalar(&motor.voltage_limit, cmd);
  25. }
  26. void setup() {
  27.   // driver config
  28.   // power supply voltage [V]
  29.   driver.voltage_power_supply = 12;
  30.   // limit the maximal dc voltage the driver can set
  31.   // as a protection measure for the low-resistance motors
  32.   // this value is fixed on startup
  33.   driver.voltage_limit = 6;
  34.   driver.init();
  35.   // link the motor and the driver
  36.   motor.linkDriver(&driver);
  37.   // limiting motor movements
  38.   // limit the voltage to be set to the motor
  39.   // start very low for high resistance motors
  40.   // current = voltage / resistance, so try to be well under 1Amp
  41.   motor.voltage_limit = 2;   // [V]
  42.   // open loop control config
  43.   motor.controller = MotionControlType::velocity_openloop;
  44.   // init motor hardware
  45.   motor.init();
  46.   // add target command T
  47.   command.add('T', doTarget, "target velocity");
  48.   command.add('L', doLimit, "voltage limit");
  49.   Serial.begin(115200);
  50.   Serial.println("电机准备就绪!");
  51.   Serial.println("请设置目标位置");
  52.   _delay(1000);
  53. }
  54. void loop() {
  55.   // open loop velocity movement
  56.   // using motor.voltage_limit and motor.velocity_limit
  57.   // to turn the motor "backwards", just set a negative target_velocity
  58.   motor.move(target_velocity);
  59.   // user communication
  60.   command.run();
  61.   _delay(1000);
  62. }
复制代码


回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 2024-7-9 09:55:15

实验二百二十二:Arduino FOC无刷电机驱动板 兼容Simple ...

实验场景图

【花雕学编程】Arduino动手做(222)---Simple FOC Shield V2.0.4图1

【花雕学编程】Arduino动手做(222)---Simple FOC Shield V2.0.4图2
回复

使用道具 举报

12
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

为本项目制作心愿单
购买心愿单
心愿单 编辑
[[wsData.name]]

硬件清单

  • [[d.name]]
btnicon
我也要做!
点击进入购买页面
上海智位机器人股份有限公司 沪ICP备09038501号-4

© 2013-2024 Comsenz Inc. Powered by Discuz! X3.4 Licensed

mail