咳咳,作为一个萌新,在这大佬云集的网站里瑟瑟发抖,但还是想发表一下最近成果:
本次主要讨论如何用单片机无线远程控制BLDC并实时的反馈数据到串口,话不多说直接上代码:
#include <stdio.h>
#define SVpin 6
#define ALMpin 4
#define BRKpin 2
#define FRpin 7
#define sensorpin 3
#define Speedpin A0
short message;
static long long count = 1;
float distance;
volatile float pulse;
volatile float sudu;
//-------------------------函数封装
class cruseCar{
private:
String situation;
public:
String report();
};
String cruseCar::report(){
int voltage=digitalRead(ALMpin);
if(voltage=LOW){
this-> situation=" malfunctioning ";
}else if(voltage=HIGH){
this-> situation=" normal ";
}
return situation;
}
cruseCar car;
//------------------------------------------
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
motor_init(); // initialize the pin
Serial.println("Backup System initializing");
Serial.println("press 1 2 3 4 5 6 for different mode");
delay(2000);
}
void loop() {
// put your main code here, to run repeatedly:
Serial.print("current speed (Km/h): ");
Serial.println(getInfo());
delay(2000);
Serial.print("current vehicle behavior: ");
Serial.println(car.report()); //汇报车辆情况
Serial.print("Now, total distance(Km): ");
Serial.println(getDistance());
if (Serial.available()>0){
message = Serial.parseInt();
switch(message){
case 1:
analogWrite(SVpin,50);
digitalWrite(BRKpin,LOW);
Serial.println("vehical on high speed mode, ");
//Serial.read();
break;
case 2:
analogWrite(SVpin,30);
digitalWrite(BRKpin,LOW);
Serial.println("vehicle on low speed mode");
//Serial.read();
break;
case 3:
digitalWrite(BRKpin,HIGH);
Serial.print("vehicle has now stopped, please check it out at reported location");
//Serial.read();
break;
case 4:
analogWrite(SVpin,15);
digitalWrite(BRKpin,LOW);
Serial.print("vehicle slowing now");
//Serial.read();
break;
case 5:
digitalWrite(FRpin,HIGH); //改变行进方向
break;
case 6:
digitalWrite(FRpin,LOW); //改变行进方向
break;
default:
digitalWrite(BRKpin,HIGH); // 当没有输入指令时,进行刹车。 作为保险项
}
}
if( digitalRead(ALMpin)==LOW){
digitalWrite(BRKpin,HIGH);
Serial.println("vehicle having problem, check out immidiately");//-----------警报信号, 开头出现一次不代表异常
}
delay(2000);
attachInterrupt(digitalPinToInterrupt(sensorpin),bianmaqi, HIGH);
}
void bianmaqi(){
count++;
}
float getInfo(){
int var=digitalRead(ALMpin);
pulse = pulseIn(Speedpin,HIGH);
sudu = 5000/(pulse*8); // 计算速度
return sudu;
}
float getDistance(){
distance = ((count/16)*PI*0.24)/1000; // 返回当前总检测公里数
return distance;
}
void motor_init(){
pinMode(BRKpin,OUTPUT);
pinMode(ALMpin,INPUT); //开始启动各项针脚
pinMode(SVpin,OUTPUT);
pinMode(FRpin,OUTPUT);
pinMode(sensorpin,INPUT);
}
//---------------------------------------------------------
//---------------------------------------------------------
//--- 铁路巡检车备用系统方案 published by Jonh-NB-Wang ---
//--- 系统仅允许被用于铁路巡检设备的控制,任何其他用途必须 ---
//--- 经过本作者审核。引用时记得要用哈佛格式。 ---
//--- written by 王铮---西南交通大学(SWJTU)---2021-7-19 ---
//---------------------------------------------------------
//---------------------------------------------------------
中间获得总里程数据时,运用了一个红外传感器(sensorpin):它通过发射并接收光,同时检测其强度发射电信号,也就是高低电平。我的设想是根据编码器的原理也DIY一个记录里程的东西。其原理就是轮毂上有均匀分布的空隙,只要吧传感器放在合适的位置,轮子转动时,空隙之间的交错出现能让传感器反馈信号。具体实现请看函数 getDistance()
一开始定义的:
#define?SVpin?6
#define?ALMpin?4
#define?BRKpin?2
#define?FRpin?7
#define?sensorpin?3
#define?Speedpin?A0
这些都是BLDC驱动器与单片机的接口信息,SV端口是输入PWM波的请注意;FR是驱动器上用来调整电机旋转方向的。speedpin是驱动器上用来读取转速(脉冲)的;BRK 就是刹车;ALM 是警报信号端口。
总的来说,虽然不算高大上,但是是可行的,可以用来控制设备运行并获取数据。
|