#include #include "HX711.h" #include "OneButton.h" #include "Serialcommand.h" #include "config.h" #include "led.h" #include //调用Ticker.h库 #include // 角度传感器 // 收放线电机控制 // 控制串口直接使用Serial2,用法和Serial一样,如需要还可以用Serial1,但需要映射引脚 //--------------------------------- OneButton button_up(BTN_UP, true); 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); button_down.attachClick(downbtn_click); button_checktop.setPressTicks(10); // 10毫秒就产生按下事件,用于顶部按钮检测 button_checktop.attachLongPressStart(ctbtn_pressstart); // 按下马上产生事件, button_checktop.attachLongPressStop(ctbtn_pressend); // 抬起 // 关闭灯光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 // 低通滤波和限幅后的拉力数值,单位:克 int get_pullweight() { float NewValue; if (scale.wait_ready_timeout(100)) // 等待数据ok,100ms超时 NewValue = scale.get_units(); else NewValue = 0; Value = NewValue * FILTER_A + (1.0 - FILTER_A) * Value; // 低通滤波 Value = constrain(Value, 0.0, 6000.0); // 限制到0-6公斤 return round(Value); } // 提示灯光控制 void led_tip(bool onled) { if (onled) digitalWrite(LED_TIP, LOW); // 亮灯 else digitalWrite(LED_TIP, HIGH); // 亮灯 } void ctbtn_pressstart() { Serial.println("ctbtn_pressstart"); led_tip(true); } void ctbtn_pressend() { Serial.println("ctbtn_pressend"); led_tip(false); } void downbtn_click() { Serial.println("downbtn_click"); } void upbtn_click() { Serial.println("upbtn_click"); }