加入多核支持,用于获取重量,通讯协议修改对应V1.0

This commit is contained in:
pxzleo 2023-04-13 19:44:23 +08:00
parent b132ed8a57
commit 191cabd3ae
8 changed files with 93 additions and 25 deletions

View File

@ -1,6 +1,7 @@
{
"files.associations": {
"cstddef": "cpp",
"algorithm": "cpp"
"algorithm": "cpp",
"cmath": "cpp"
}
}

View File

@ -17,3 +17,4 @@ upload-port = COM[14]
lib_deps =
bogde/HX711@^0.7.5
mathertel/OneButton@^2.0.3
sstaub/Ticker@^4.4.0

View File

@ -112,11 +112,29 @@ bool Serialcommand::checksum()
}
return bufsum == chksum;
}
void Serialcommand::sendinfo(HookStatus status,int linelength,int pullweight )
{
// 长度
outBuffer[2] = 0x07;
outBuffer[3] = 0x02;
outBuffer[4] = status;
outBuffer[5] =(uint8_t) ((linelength>>8) &0xff);
outBuffer[6] = (uint8_t) (linelength&0xff);
outBuffer[7] =(uint8_t) ((pullweight>>8) &0xff);
outBuffer[8] = (uint8_t) (pullweight&0xff);
outBuffer[9] = getsum(outBuffer + 2, 7);
Serial2.write(outBuffer, 10);
}
void Serialcommand::sendcmdret(uint8_t cmdret)
{
// 长度
outBuffer[2] = 0x02;
outBuffer[2] = 0x03;
// 标识命令返回
outBuffer[3] = 0x01;
// 返回数据

View File

@ -1,4 +1,5 @@
#include "Arduino.h"
#include "config.h"
class Serialcommand
{
@ -21,4 +22,5 @@ public:
Serialcommand(); // 构造函数
~Serialcommand(); // 析构函数
void getcommand();
void sendinfo(HookStatus status,int linelength,int pullweight );
};

View File

@ -1,5 +1,10 @@
//硬件接口定义文件
#pragma once
////////////
//定义公共结构,变量,硬件接口等
///
//
//硬件接口定义////////////////////////////
// 按钮
#define BTN_UP 23 // 收线开关 接线BTN_UP---GND
#define BTN_DOWN 22 // 放线开关
@ -8,4 +13,15 @@
#define LED_TIP 19 // 指示灯 接线LED_TIP---3.3V
// 称重传感器- HX711
#define LOADCELL_DOUT_PIN 2
#define LOADCELL_SCK_PIN 4
#define LOADCELL_SCK_PIN 4
///////////////////////////////////////////
// 挂钩状态
enum HookStatus
{
HS_Unknow, // 未知状态
HS_Lock, // 已入仓并锁定
HS_Down, // 自动下放中
HS_Up, // 回收中
HS_Inbox // 缓慢入仓中(货物重量测量相对准确)
};

0
src/led.cpp Normal file
View File

0
src/led.h Normal file
View File

View File

@ -1,9 +1,11 @@
#include <Arduino.h>
#include "HX711.h"
#include "OneButton.h"
#include "Serialcommand.h"
#include "config.h"
#include "led.h"
#include <Ticker.h> //调用Ticker.h库
#include <soc/rtc_cntl_reg.h>
// 角度传感器
// 收放线电机控制
@ -15,19 +17,31 @@ OneButton button_down(BTN_DOWN, true);
OneButton button_checktop(BTN_CT, true);
HX711 scale;
Serialcommand sercomm;
void sendinfo();
Ticker tksendinfo(sendinfo, 500); // 发送状态
HookStatus hookstatus=HS_Unknow;
void upbtn_click();
void downbtn_click();
void ctbtn_pressend();
void ctbtn_pressstart();
void led_tip(bool onled);
int get_pullweight();
void Task1(void *pvParameters);
//void Task1code( void *pvParameters );
int pullweight=0;
void setup()
{
WRITE_PERI_REG(RTC_CNTL_BROWN_OUT_REG, 0);//关闭低电压检测,避免无限重启
//启用Esp32双核第二个核心
xTaskCreatePinnedToCore(Task1, "Task1", 10000, NULL, 1, NULL, 0); //最后一个参数至关重要,决定这个任务创建在哪个核上.PRO_CPU 为 0, APP_CPU 为 1,或者 tskNO_AFFINITY 允许任务在两者上运行.
// 调试串口
Serial.begin(115200);
Serial.println("Starting PullupDevice...");
// 初始化按钮
button_up.attachClick(upbtn_click);
@ -35,21 +49,49 @@ void setup()
button_checktop.setPressTicks(10); // 10毫秒就产生按下事件用于顶部按钮检测
button_checktop.attachLongPressStart(ctbtn_pressstart); // 按下马上产生事件,
button_checktop.attachLongPressStop(ctbtn_pressend); // 抬起
// 初始化称重传感器
scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
scale.set_scale(516.f); // 这是缩放值,根据砝码实测
scale.tare(); // 重置为0
// 关闭灯光LED
pinMode(LED_TIP, OUTPUT);
led_tip(false);
tksendinfo.start();
Serial.println("PullupDevice is ready!");
}
//在核心1上执行重要的延迟低的
void loop()
{
tksendinfo.update();
button_checktop.tick();
button_down.tick();
button_up.tick();
sercomm.getcommand();
if (pullweight>10)
printf("PullWeight:%d\n",pullweight);
delay(10);
}
//在核心0上执行耗时长的低优先级的
void Task1(void *pvParameters) {
// 初始化称重传感器
scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
scale.set_scale(516.f); // 这是缩放值,根据砝码实测
scale.tare(); // 重置为0
//在这里可以添加一些代码这样的话这个任务执行时会先执行一次这里的内容当然后面进入while循环之后不会再执行这部分了
while(1)
{
pullweight=get_pullweight();
vTaskDelay(10);
}
}
void sendinfo() // 每500ms发送状态信息
{
sercomm.sendinfo(hookstatus,5324,pullweight);
}
float Value;
#define FILTER_A 0.5
// 低通滤波和限幅后的拉力数值,单位:克
float Get_PullUnits()
int get_pullweight()
{
float NewValue;
if (scale.wait_ready_timeout(100)) // 等待数据ok,100ms超时
@ -58,7 +100,7 @@ float Get_PullUnits()
NewValue = 0;
Value = NewValue * FILTER_A + (1.0 - FILTER_A) * Value; // 低通滤波
Value = constrain(Value, 0.0, 6000.0); // 限制到0-6公斤
return Value;
return round(Value);
}
// 提示灯光控制
void led_tip(bool onled)
@ -69,18 +111,6 @@ void led_tip(bool onled)
digitalWrite(LED_TIP, HIGH); // 亮灯
}
void loop()
{
button_checktop.tick();
button_down.tick();
button_up.tick();
sercomm.getcommand();
delay(10);
// Serial.print("PullUnits:\t");
// Serial.println(Get_PullUnits(), 1);
}
void ctbtn_pressstart()
{