From e43b0bc5a70752a74d815bf311b01b7f78e578cc Mon Sep 17 00:00:00 2001 From: applenana <83851967+applenana@users.noreply.github.com> Date: Fri, 21 Jun 2024 18:04:56 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B0=9D=E8=AF=95=E4=B8=BB=E5=8A=A8=E6=9A=82?= =?UTF-8?q?=E5=81=9C(=E5=A4=B1=E8=B4=A5=E5=AD=98=E6=A1=A3)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Arduino/src/main.cpp | 93 ++++++++++++++++++++++++++------------------ 1 file changed, 55 insertions(+), 38 deletions(-) diff --git a/Arduino/src/main.cpp b/Arduino/src/main.cpp index ae0e480..7ac0e0d 100644 --- a/Arduino/src/main.cpp +++ b/Arduino/src/main.cpp @@ -24,7 +24,7 @@ String ha_mqtt_password; //-=-=-=-=-=-↓系统配置↓-=-=-=-=-=-=-=-=-= bool debug = false; -String sw_version = "v2.0"; +String sw_version = "v2.0.1"; String bambu_mqtt_user = "bblp"; String bambu_mqtt_id = "ams"; String ha_mqtt_id = "ams"; @@ -37,6 +37,8 @@ String bambu_load = "{\"print\":{\"command\":\"ams_change_filament\",\"curr_temp String bambu_done = "{\"print\":{\"command\":\"ams_control\",\"param\":\"done\",\"sequence_id\":\"1\"},\"user_id\":\"1\"}"; // 完成 String bambu_clear = "{\"print\":{\"command\": \"clean_print_error\",\"sequence_id\":\"1\"},\"user_id\":\"1\"}"; String bambu_status = "{\"pushing\": {\"sequence_id\": \"0\", \"command\": \"pushall\"}}"; +//String bambu_pause = "{\"print\": {\"command\": \"gcode_line\",\"sequence_id\": \"1\",\"param\": \"M400U1\"},\"user_id\": \"1\"}"; +String bambu_pause = "{\"print\":{\"command\":\"pause\",\"sequence_id\":\"1\"},\"user_id\":\"1\"}"; int servoPin = 13;//舵机引脚 int motorPin1 = 4;//电机一号引脚 int motorPin2 = 5;//电机二号引脚 @@ -57,6 +59,7 @@ unsigned int ledBrightness;//led默认亮度 //String subStep; bool unloadMsg; bool completeMSG; +bool reSendUnload; //-=-=-=-=-=-=end unsigned long lastMsg = 0; @@ -238,6 +241,11 @@ class ServoControl { ServoControl sv; Machinery mc(motorPin1, motorPin2); +void statePublish(String content){ + Serial.println(content); + haClient.publish(("AMS/"+filamentID+"/state").c_str(),content.c_str()); +} + //连接拓竹mqtt void connectBambuMQTT() { int count = 1; @@ -295,11 +303,12 @@ void bambuCallback(char* topic, byte* payload, unsigned int length) { if (!(amsStatus == printError && printError == hwSwitchState && hwSwitchState == gcodeState && gcodeState == mcPercent && mcPercent == mcRemainingTime && mcRemainingTime == "null")) { if (debug){ - Serial.println(sequenceId+"|ams["+amsStatus+"]"+"|err["+printError+"]"+"|hw["+hwSwitchState+"]"+"|gcode["+gcodeState+"]"+"|mcper["+mcPercent+"]"+"|mcrtime["+mcRemainingTime+"]"); - Serial.print("Free memory: "); - Serial.print(ESP.getFreeHeap()); - Serial.println(" bytes"); - Serial.println("-=-=-=-=-");} + statePublish(sequenceId+"|ams["+amsStatus+"]"+"|err["+printError+"]"+"|hw["+hwSwitchState+"]"+"|gcode["+gcodeState+"]"+"|mcper["+mcPercent+"]"+"|mcrtime["+mcRemainingTime+"]"); + //Serial.print("Free memory: "); + //Serial.print(ESP.getFreeHeap()); + //Serial.println(" bytes"); + statePublish("Free memory: "+String(ESP.getFreeHeap())+" bytes"); + statePublish("-=-=-=-=-");} bambuLastTime = millis(); } @@ -314,39 +323,42 @@ void bambuCallback(char* topic, byte* payload, unsigned int length) { */ JsonDocument Pdata = getPData(); if (Pdata["step"] == "1"){ - if (gcodeState == "PAUSE" and mcPercent.toInt() > 100){ - Serial.println("收到换色指令,进入换色准备状态"); + if (mcPercent.toInt() > 100){ + statePublish("收到换色指令,进入换色准备状态"); leds.setPixelColor(2,leds.Color(255,0,0)); leds.setPixelColor(1,leds.Color(0,255,0)); leds.setPixelColor(0,leds.Color(0,0,255)); leds.show(); + String nextFilament = String(mcPercent.toInt() - 110 + 1); Pdata["nextFilament"] = nextFilament; unloadMsg = false; completeMSG = false; + reSendUnload = false; sv.pull(); mc.stop(); - Serial.println("本机通道"+String(Pdata["filamentID"])+"|上料通道"+String(Pdata["lastFilament"])+"|下一耗材通道"+nextFilament); + statePublish("本机通道"+String(Pdata["filamentID"])+"|上料通道"+String(Pdata["lastFilament"])+"|下一耗材通道"+nextFilament); if (Pdata["filamentID"] == Pdata["lastFilament"]){ - Serial.println("本机通道["+String(Pdata["filamentID"])+"]在上料");//如果处于上料状态,那么对于这个换色单元来说,接下来只能退料或者继续打印(不退料) + statePublish("本机通道["+String(Pdata["filamentID"])+"]在上料");//如果处于上料状态,那么对于这个换色单元来说,接下来只能退料或者继续打印(不退料) if (nextFilament == Pdata["filamentID"]){ - Serial.println("本机通道,上料通道,下一耗材通道全部相同!无需换色!"); + statePublish("本机通道,上料通道,下一耗材通道全部相同!无需换色!"); Pdata["step"] = "5"; Pdata["subStep"] = "1"; }else{ - Serial.println("下一耗材通道与本机通道不同,需要换料,准备退料"); + bambuClient.publish(bambu_topic_publish.c_str(),bambu_pause.c_str()); + statePublish("下一耗材通道与本机通道不同,需要换料,准备退料"); Pdata["step"] = "2"; Pdata["subStep"] = "1"; } }else{ - Serial.println("本机通道["+String(Pdata["filamentID"])+"]不在上料");//如果本换色单元不在上料,那么又两个可能,要么本次换色与自己无关,要么就是要准备进料 + statePublish("本机通道["+String(Pdata["filamentID"])+"]不在上料");//如果本换色单元不在上料,那么又两个可能,要么本次换色与自己无关,要么就是要准备进料 if (nextFilament == Pdata["filamentID"]){ - Serial.println("本机通道将要换色,准备送料"); + statePublish("本机通道将要换色,准备送料"); Pdata["step"] = String("3"); Pdata["subStep"] = String("1"); }else{ - Serial.println("本机通道与本次换色无关,无需换色"); + statePublish("本机通道与本次换色无关,无需换色"); Pdata["step"] = String("4"); Pdata["subStep"] = String("1"); } @@ -354,29 +366,34 @@ void bambuCallback(char* topic, byte* payload, unsigned int length) { }else{ledAll(0,0,255);} }else if (Pdata["step"] == "2"){ if (Pdata["subStep"] == "1"){ - Serial.println("进入退料状态"); + statePublish("进入退料状态"); leds.clear(); leds.setPixelColor(2,leds.Color(255,255,255)); leds.show(); - bambuClient.publish(bambu_topic_publish.c_str(),bambu_unload.c_str()); + //bambuClient.publish(bambu_topic_publish.c_str(),bambu_unload.c_str()); Pdata["subStep"] = "2"; }else if (Pdata["subStep"] == "2"){ leds.setPixelColor(1,leds.Color(255,255,255)); leds.show(); - if (printError == "318750723") { - Serial.println("拔出耗材"); + if (not reSendUnload){ + reSendUnload = true; + delay(3000); + statePublish("仍未退料,发出退料请求"); + bambuClient.publish(bambu_topic_publish.c_str(),bambu_unload.c_str()); + }else if (printError == "318750723") { + statePublish("拔出耗材"); sv.push(); mc.backforward(); Pdata["subStep"] = "3"; } else if (printError == "318734339") { - Serial.println("拔出耗材"); + statePublish("拔出耗材"); sv.push(); mc.backforward(); bambuClient.publish(bambu_topic_publish.c_str(), bambu_resume.c_str()); Pdata["subStep"] = "3"; } }else if (Pdata["subStep"] == "3" && amsStatus == "0"){ - Serial.println("退料完成,本次换色完成"); + statePublish("退料完成,本次换色完成"); leds.setPixelColor(2,leds.Color(255,255,255)); leds.show(); delay(backTime); @@ -388,7 +405,7 @@ void bambuCallback(char* topic, byte* payload, unsigned int length) { }else if (Pdata["step"] == "3"){ if (Pdata["subStep"] == "1"){ if (amsStatus == "0") { - Serial.println("进入送料状态"); + statePublish("进入送料状态"); leds.clear(); leds.setPixelColor(2,leds.Color(255,255,0)); leds.show(); @@ -396,7 +413,7 @@ void bambuCallback(char* topic, byte* payload, unsigned int length) { Pdata["subStep"] = "2"; // 更新 subStep } else { if (!unloadMsg){ - Serial.println("等待耗材退料完成……"); + statePublish("等待耗材退料完成……"); unloadMsg = true; }else{ Serial.print("."); @@ -413,7 +430,7 @@ void bambuCallback(char* topic, byte* payload, unsigned int length) { } } }else if (Pdata["subStep"] == "2" && printError == "318750726"){ - Serial.println("送入耗材"); + statePublish("送入耗材"); sv.push(); mc.forward(); Pdata["subStep"] = "3"; @@ -423,7 +440,7 @@ void bambuCallback(char* topic, byte* payload, unsigned int length) { leds.setPixelColor(1,leds.Color(255,255,0)); leds.show(); }else if ((Pdata["subStep"] == "3" && amsStatus == "262" && hwSwitchState == "1") or digitalRead(bufferPin1) == 1){ - Serial.println("停止送料"); + statePublish("停止送料"); mc.stop(); bambuClient.publish(bambu_topic_publish.c_str(),bambu_done.c_str()); Pdata["subStep"] = "4"; @@ -432,18 +449,18 @@ void bambuCallback(char* topic, byte* payload, unsigned int length) { leds.show(); }else if (Pdata["subStep"] == "4"){ if (hwSwitchState == "0") { - Serial.println("检测到送料失败,重新送料!"); + statePublish("检测到送料失败,重新送料!"); mc.backforward(); delay(2000); mc.stop(); - Pdata["subStep"] = "1"; + Pdata["subStep"] = "2"; leds.setPixelColor(2,leds.Color(255,255,0)); leds.setPixelColor(1,leds.Color(255,0,0)); leds.show(); } else if (hwSwitchState == "1") { - Serial.println("送料成功,等待挤出换料"); - sv.pull(); + statePublish("送料成功,等待挤出换料"); + sv.pull(); Pdata["subStep"] = "5"; // 更新 subStep leds.setPixelColor(2,leds.Color(255,255,0)); @@ -453,14 +470,14 @@ void bambuCallback(char* topic, byte* payload, unsigned int length) { }else if (Pdata["subStep"] == "5"){ if (printError == "318734343") { if (hwSwitchState == "1"){ - Serial.println("被虚晃一枪!重新点击确认"); + statePublish("被虚晃一枪!重新点击确认"); bambuClient.publish(bambu_topic_publish.c_str(), bambu_done.c_str()); leds.setPixelColor(2,leds.Color(255,255,0)); leds.setPixelColor(1,leds.Color(0,255,0)); leds.setPixelColor(0,leds.Color(0,255,0)); leds.show(); }else if (hwSwitchState == "0"){ - Serial.println("检测到送料失败……进入步骤AGAIN重新送料"); + statePublish("检测到送料失败……进入步骤AGAIN重新送料"); leds.setPixelColor(2,leds.Color(255,255,0)); leds.setPixelColor(1,leds.Color(255,0,0)); leds.setPixelColor(0,leds.Color(255,0,0)); @@ -472,7 +489,7 @@ void bambuCallback(char* topic, byte* payload, unsigned int length) { Pdata["subStep"] = "AGAIN"; } } else if (amsStatus == "768") { - Serial.println("芜湖~换料完成!"); + statePublish("芜湖~换料完成!"); ledAll(0,255,0); delay(1000); bambuClient.publish(bambu_topic_publish.c_str(), bambu_resume.c_str()); @@ -481,10 +498,10 @@ void bambuCallback(char* topic, byte* payload, unsigned int length) { } }else if (Pdata["subStep"] == "AGAIN"){ if (hwSwitchState == "0"){ - Serial.println("尝试重新送料"); + statePublish("尝试重新送料"); mc.forward(); }else if (hwSwitchState == "1"){ - Serial.println("送料成功!"); + statePublish("送料成功!"); mc.stop(); sv.pull(); Pdata["subStep"] = "5"; @@ -492,7 +509,7 @@ void bambuCallback(char* topic, byte* payload, unsigned int length) { } }else if (Pdata["step"] == "4"){ if (!completeMSG){ - Serial.println("进入看戏状态,等待换色完成"); + statePublish("进入看戏状态,等待换色完成"); completeMSG = true; }else{ Serial.print("."); @@ -510,7 +527,7 @@ void bambuCallback(char* topic, byte* payload, unsigned int length) { if (amsStatus == "1280" and gcodeState != "PAUSE") { String nextFilament = Pdata["nextFilament"]; - Serial.println("换色完成!切换上料通道为["+nextFilament+"]"); + statePublish("换色完成!切换上料通道为["+nextFilament+"]"); Pdata["step"] = "1"; Pdata["subStep"] = "1"; Pdata["lastFilament"] = nextFilament; @@ -519,7 +536,7 @@ void bambuCallback(char* topic, byte* payload, unsigned int length) { }else if (Pdata["step"] == "5"){ bambuClient.publish(bambu_topic_publish.c_str(),bambu_resume.c_str()); if (!completeMSG){ - Serial.println("发送继续指令"); + statePublish("发送继续指令"); completeMSG = true; }else{ Serial.print("."); @@ -536,7 +553,7 @@ void bambuCallback(char* topic, byte* payload, unsigned int length) { } if (amsStatus == "1280") { - Serial.println("完成!"); + statePublish("完成!"); Pdata["step"] = "1"; Pdata["subStep"] = "1"; ledAll(0,255,0);