Skip to content

Commit

Permalink
尝试主动暂停(失败存档)
Browse files Browse the repository at this point in the history
  • Loading branch information
applenana committed Jun 21, 2024
1 parent 5106b36 commit e43b0bc
Showing 1 changed file with 55 additions and 38 deletions.
93 changes: 55 additions & 38 deletions Arduino/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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;//电机二号引脚
Expand All @@ -57,6 +59,7 @@ unsigned int ledBrightness;//led默认亮度
//String subStep;
bool unloadMsg;
bool completeMSG;
bool reSendUnload;
//-=-=-=-=-=-=end

unsigned long lastMsg = 0;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}

Expand All @@ -314,69 +323,77 @@ 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");
}
}
}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);
Expand All @@ -388,15 +405,15 @@ 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();
bambuClient.publish(bambu_topic_publish.c_str(), bambu_load.c_str());
Pdata["subStep"] = "2"; // 更新 subStep
} else {
if (!unloadMsg){
Serial.println("等待耗材退料完成……");
statePublish("等待耗材退料完成……");
unloadMsg = true;
}else{
Serial.print(".");
Expand All @@ -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";
Expand All @@ -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";
Expand All @@ -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));
Expand All @@ -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));
Expand All @@ -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());
Expand All @@ -481,18 +498,18 @@ 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";
}
}
}else if (Pdata["step"] == "4"){
if (!completeMSG){
Serial.println("进入看戏状态,等待换色完成");
statePublish("进入看戏状态,等待换色完成");
completeMSG = true;
}else{
Serial.print(".");
Expand All @@ -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;
Expand All @@ -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(".");
Expand All @@ -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);
Expand Down

0 comments on commit e43b0bc

Please sign in to comment.