利用手机蓝牙发送串口数据对直流无刷电机进行控制

电机控制代码基于simplefoc库

硬件使用:

  1. 电机:直流无刷电机2804
  2. 单片机MCU:ESP32 lolin D32
  3. 编码器:AS5600
  4. 驱动板:simplefoc mini
  5. 导线:杜邦线若干
  6. 安卓手机,安装蓝牙串口助手app
  7. 使用平台: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;

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

M创动工坊

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值