Skip to content

Commit

Permalink
FAMSL-beta-v1.3源码
Browse files Browse the repository at this point in the history
  • Loading branch information
applenana committed Aug 20, 2024
1 parent 8ae7952 commit 45ff7b0
Show file tree
Hide file tree
Showing 10 changed files with 179 additions and 93 deletions.
Binary file modified Arduino/.pio/build/esp12e/src/dataPacket.cpp.o
Binary file not shown.
Binary file modified Arduino/.pio/build/esp12e/src/led.cpp.o
Binary file not shown.
Binary file modified Arduino/.pio/build/esp12e/src/main.cpp.o
Binary file not shown.
Binary file modified Arduino/.pio/build/esp12e/src/processData.cpp.o
Binary file not shown.
9 changes: 5 additions & 4 deletions Arduino/include/dataPacket.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "CRC.h"
#include "Motor.h"
#include "ServoMotor.h"
#include "led.h"
#pragma once

class DataPacket {
Expand All @@ -9,11 +10,11 @@ class DataPacket {
CRC16 crc16;
public:
uint8_t index; // 索引
uint8_t sequenceNumber; // 序号
uint8_t address; // 地址
int8_t sequenceNumber; // 序号
int8_t address; // 地址
uint8_t length; // 长度
uint8_t commandType; // 命令号
uint8_t content; // 内容(一字节)
byte content[512]; // 内容(至多512字节)
uint8_t headCRC; // 头检验
uint8_t totalLowCRC; // 总低检验
uint8_t totalHighCRC; // 总高检验
Expand All @@ -22,5 +23,5 @@ class DataPacket {
DataPacket(); // 构造函数声明
bool headCheck();
bool totalCheck();
void sendPacket();
void sendPacket(bool ISIO2OUT = false);
};
11 changes: 4 additions & 7 deletions Arduino/include/led.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
#include <Adafruit_NeoPixel.h>
#pragma once
#include <Adafruit_NeoPixel.h>

class Led{
public:
Led(int ledPixels,int ledPin);
Adafruit_NeoPixel leds;
void All(unsigned int r, unsigned int g, unsigned int b);
};
extern Adafruit_NeoPixel leds;
void ledAll(unsigned int r,unsigned int g,unsigned int b);
void ledPC(uint pixels,uint r,uint g,uint b);
44 changes: 36 additions & 8 deletions Arduino/src/dataPacket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ DataPacket::DataPacket()
: crc8(0x39, 0x66, 0x00, false, false),
crc16(0x1021, 0x913D, 0x0000, false, false)
{
// 可以在这里初始化其他成员变量
}

bool DataPacket::headCheck(){
Expand All @@ -16,26 +15,55 @@ bool DataPacket::headCheck(){
}

bool DataPacket::totalCheck(){
byte packet[] = {0x3D,sequenceNumber,address,length,headCRC,commandType,content};
byte* packet = new byte[length - 2];
packet[0] = 0x3D;
packet[1] = sequenceNumber;
packet[2] = address;
packet[3] = length;
packet[4] = headCRC;
packet[5] = commandType;
memcpy(packet + 6,content,length - 8);
crc16.restart();
crc16.add(packet, sizeof(packet)); // 计算前 12 个字节的 CRC16 校验
crc16.add(packet, length - 2);
uint16_t totalCRC = crc16.calc();
uint8_t lowCRC = totalCRC & 0xFF; // 低字节
uint8_t highCRC = (totalCRC >> 8) & 0xFF; // 高字节
delete[] packet;
return (lowCRC == totalLowCRC and highCRC == totalHighCRC);
}

void DataPacket::sendPacket(){
void DataPacket::sendPacket(bool ISIO2OUT){
byte head[] = {0x3D,sequenceNumber,address,length};
crc8.restart();
crc8.add(head,4);
headCRC = crc8.calc();
byte packet[] = {0x3D,sequenceNumber,address,length,headCRC,commandType,content};
byte* packet = new byte[length];
packet[0] = 0x3D;
packet[1] = sequenceNumber;
packet[2] = address;
packet[3] = length;
packet[4] = headCRC;
packet[5] = commandType;
memcpy(packet + 6,content,length - 8);
crc16.restart();
crc16.add(packet, sizeof(packet)); // 计算前 12 个字节的 CRC16 校验
crc16.add(packet, length - 2);
uint16_t totalCRC = crc16.calc();
totalLowCRC = totalCRC & 0xFF; // 低字节
totalHighCRC = (totalCRC >> 8) & 0xFF; // 高字节
byte finalPacket[] = {0x3D,sequenceNumber,address,length,headCRC,commandType,content,totalLowCRC,totalHighCRC};
Serial.write(finalPacket, sizeof(finalPacket));
packet[length - 2] = totalLowCRC;
packet[length - 1] = totalHighCRC;
if (ISIO2OUT){
pinMode(2,SPECIAL);
Serial1.write(packet, length);
//delay(1);
Serial1.flush();
pinMode(2,INPUT);
ledPC(1,0,255,0);
}else{
Serial.write(packet, length);
ledPC(1,0,0,255);
}
delay(1);
delete[] packet;
ledPC(1,0,0,0);
}
9 changes: 5 additions & 4 deletions Arduino/src/led.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
#include "led.h"

Led::Led(int ledPixels,int ledPin){
Adafruit_NeoPixel leds(ledPixels, ledPin, NEO_GRB + NEO_KHZ800);
void ledAll(unsigned int r,unsigned int g,unsigned int b){
leds.fill(leds.Color(r,g,b));
leds.show();
}

void Led::All(unsigned int r, unsigned int g, unsigned int b){
leds.fill(leds.Color(r,g,b));
void ledPC(uint pixels,uint r,uint g,uint b){
leds.setPixelColor(pixels,leds.Color(r,g,b));
leds.show();
}
173 changes: 112 additions & 61 deletions Arduino/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,83 +3,134 @@
#include "processData.h"
#include "ServoMotor.h"
#include "Motor.h"
#include <Adafruit_NeoPixel.h>
#include "led.h"

DataPacket datapacket;
ServoMotor sv(13); //构建舵机对象(选择13引脚为pwm输出引脚)
Motor mc(4,5); //构建电机对象(选择4和5引脚控制电机驱动)
Adafruit_NeoPixel leds(3, 12, NEO_GRB + NEO_KHZ800);//构建leds对象(3个led,12号引脚)
unsigned int FilamentIoPin = A0; //断料检测引脚(ADC=>A0)
String FilamentState = "none"; //断料状态
bool IsFPinHigh;//ADC是否高电平

void FilamentChange();//断料检测中断函数
void FilamentChange(bool ToStateIsHigh);

void setup() {
Serial.begin(112500);
Serial.begin(115200,SERIAL_8E1);
Serial1.begin(115200,SERIAL_8E1);
leds.begin();

//设置断料检测
pinMode(FilamentIoPin, INPUT_PULLDOWN_16);
attachInterrupt(FilamentIoPin,FilamentChange,CHANGE);
IsFPinHigh = (analogRead(A0) > 1000);
if (IsFPinHigh){FilamentState = "exist";}
else{FilamentState = "inexist";}
leds.setBrightness(100);
leds.clear();
leds.show();
}

void loop() {
if (datapacket.index == 0 and Serial.read() == 0x3D){
datapacket.index += 1;
}else if (Serial.read() != -1 and datapacket.index > 0 and datapacket.index <= 4){
switch (datapacket.index){
case 1:
datapacket.sequenceNumber = Serial.read();
break;
case 2:
datapacket.address = Serial.read();
break;
case 3:
datapacket.length = Serial.read();
break;
case 4:
datapacket.headCRC = Serial.read();
break;
if ((analogRead(A0) > 1000) != IsFPinHigh){
FilamentChange(not IsFPinHigh);
IsFPinHigh = not IsFPinHigh;
}
datapacket.index += 1;
}else if (Serial.read() != -1 and datapacket.index == 5){
if (datapacket.totalCheck()){
datapacket.content = Serial.read();
datapacket.contentlength += 1;
datapacket.index += 1;
}else{
datapacket.index = 0;
}
}else if (Serial.read() != -1 and datapacket.index >= datapacket.length - 2 and datapacket.index < datapacket.length){
datapacket.index += 1;
if (datapacket.index == datapacket.length - 2){
datapacket.totalLowCRC = Serial.read();
}else if (datapacket.index == datapacket.length -1){
datapacket.totalHighCRC = Serial.read();
}
}else if (datapacket.index == datapacket.length){
if (datapacket.totalCheck()){
processData(datapacket);
datapacket.index = 0;
}else{
datapacket.index = 0;

if (datapacket.index == 0 and Serial.read() == 0x3D){
//Serial.println("帧头");
datapacket.index += 1;
datapacket.contentlength = 0;
ledPC(0,0,255,0);
}else if (Serial.available() and datapacket.index > 0 and datapacket.index <= 4){
//Serial.println("读取序号,地址,长度,headCRC");
switch (datapacket.index){
case 1:
datapacket.sequenceNumber = Serial.read();
//Serial.println(datapacket.sequenceNumber,HEX);
break;
ledPC(0,0,0,0);
case 2:
datapacket.address = Serial.read();
//Serial.println(datapacket.address,HEX);
ledPC(0,0,255,0);
break;
case 3:
datapacket.length = Serial.read();
//Serial.println(datapacket.length,HEX);
ledPC(0,0,0,0);
break;
case 4:
datapacket.headCRC = Serial.read();
//Serial.println(datapacket.headCRC,HEX);
ledPC(0,0,255,0);
break;
}
datapacket.index += 1;
}else if (Serial.available() and datapacket.index == 5){
//Serial.println("检验头");
if (datapacket.headCheck()){
datapacket.commandType = Serial.read();
datapacket.index += 1;
//Serial.println("检验头success|读取命令号");
//Serial.println(datapacket.commandType,HEX);
ledPC(0,0,0,0);
}else{
datapacket.index = 0;
//Serial.println("检验头失败");
ledPC(0,255,0,0);
}
}else if (Serial.available() and datapacket.index >= 6 and datapacket.index <= datapacket.length - 3){
//Serial.println("读取内容");
ledPC(0,0,255,0);
datapacket.content[datapacket.contentlength] = Serial.read();
//Serial.println(datapacket.content[datapacket.contentlength],HEX);
datapacket.index += 1;
datapacket.contentlength += 1;
}else if (Serial.available() and datapacket.index >= datapacket.length - 2 and datapacket.index < datapacket.length){
//Serial.println("读取总CRC");
if (datapacket.index == datapacket.length - 2){
datapacket.totalLowCRC = Serial.read();
//Serial.println("读取低CRC");
//Serial.println(datapacket.totalLowCRC,HEX);
ledPC(0,0,0,0);
}else if (datapacket.index == datapacket.length -1){
datapacket.totalHighCRC = Serial.read();
//Serial.println("读取高CRC");
//Serial.println(datapacket.totalHighCRC,HEX);
ledPC(0,0,255,0);
}
datapacket.index += 1;
}else if (datapacket.index == datapacket.length and datapacket.index != 0){
//Serial.println("进入最后处理");
//Serial.println(FilamentState);
if (datapacket.totalCheck()){
//Serial.println("进入最后处理,success");
ledPC(0,0,0,0);
processData(datapacket);
datapacket.index = 0;
}else{
//Serial.println("进入最后处理,fail");
datapacket.index = 0;
ledPC(0,255,0,0);
}
}
}
}

void FilamentChange(){
if (digitalRead(FilamentIoPin) == HIGH){
//低变高-无变有
if (FilamentState == "none" or FilamentState == "inexist"){
FilamentState = "exist";
}else if (FilamentState == "busy"){
mc.stop();
sv.pull();
FilamentState = "exist";
void FilamentChange(bool ToStateIsHigh){
if (ToStateIsHigh){
//低变高-无变有
if (FilamentState == "none" or FilamentState == "inexist"){
FilamentState = "exist";
}else if (FilamentState == "busy"){
mc.stop();
sv.pull();
FilamentState = "exist";
}
}else if(digitalRead(FilamentIoPin) == LOW){
//高变低-有变无
if (FilamentState == "none" or FilamentState == "exist"){
FilamentState = "inexist";
}else if (FilamentState == "busy"){
mc.forward();
}
}
}else if(not ToStateIsHigh){
//高变低-有变无
if (FilamentState == "none" or FilamentState == "exist"){
FilamentState = "inexist";
}else if (FilamentState == "busy"){
mc.forward();
}
}
}
26 changes: 17 additions & 9 deletions Arduino/src/processData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,22 @@
void processData(DataPacket data){
if (data.sequenceNumber >= data.address){
//执行指令并回复
uint8_t originLength = data.length;
uint8_t originCommandType = data.commandType;
byte originContent[512];
memcpy(originContent, data.content, 512);// 复制内容

switch (data.commandType){
case 0x00:
data.length = 0x09;
data.commandType = 0x80;
if (FilamentState == "inexist"){data.content = 0x0F;}
else if (FilamentState == "exist"){data.content = 0xF0;}
else if (FilamentState == "busy"){data.content = 0xF1;}
data.sendPacket();

if (FilamentState == "inexist"){data.content[0] = 0x0F;}
else if (FilamentState == "exist"){data.content[0] = 0xF0;}
else if (FilamentState == "busy"){data.content[0] = 0xF1;}
data.sendPacket(true);
break;
case 0x01:
switch (data.content){
switch (data.content[0]){
case 0xF0://送出
sv.push();
mc.forward();
Expand All @@ -34,14 +38,18 @@ void processData(DataPacket data){

data.length = 0x09;
data.commandType = 0x81;
data.content = 0x00;
data.sendPacket();
data.content[0] = 0x00;
data.sendPacket(true);
//发送回应数据包

break;
}
}

data.length = originLength;
data.commandType = originCommandType;
memcpy(data.content, originContent, 512);
}

if (data.sequenceNumber != data.address){
//转发数据包
data.sequenceNumber += 1;
Expand Down

0 comments on commit 45ff7b0

Please sign in to comment.