From 818afa73a110d9a41e5a913c60f881da2768cb65 Mon Sep 17 00:00:00 2001 From: pxzleo Date: Tue, 11 Apr 2023 19:16:14 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=9D=E6=AC=A1=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 5 ++ .vscode/extensions.json | 10 +++ include/README | 39 +++++++++ lib/README | 46 +++++++++++ platformio.ini | 19 +++++ src/main.cpp | 178 ++++++++++++++++++++++++++++++++++++++++ test/README | 11 +++ 7 files changed, 308 insertions(+) create mode 100644 .gitignore create mode 100644 .vscode/extensions.json create mode 100644 include/README create mode 100644 lib/README create mode 100644 platformio.ini create mode 100644 src/main.cpp create mode 100644 test/README diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..89cc49c --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +.pio +.vscode/.browse.c_cpp.db* +.vscode/c_cpp_properties.json +.vscode/launch.json +.vscode/ipch diff --git a/.vscode/extensions.json b/.vscode/extensions.json new file mode 100644 index 0000000..080e70d --- /dev/null +++ b/.vscode/extensions.json @@ -0,0 +1,10 @@ +{ + // See http://go.microsoft.com/fwlink/?LinkId=827846 + // for the documentation about the extensions.json format + "recommendations": [ + "platformio.platformio-ide" + ], + "unwantedRecommendations": [ + "ms-vscode.cpptools-extension-pack" + ] +} diff --git a/include/README b/include/README new file mode 100644 index 0000000..194dcd4 --- /dev/null +++ b/include/README @@ -0,0 +1,39 @@ + +This directory is intended for project header files. + +A header file is a file containing C declarations and macro definitions +to be shared between several project source files. You request the use of a +header file in your project source file (C, C++, etc) located in `src` folder +by including it, with the C preprocessing directive `#include'. + +```src/main.c + +#include "header.h" + +int main (void) +{ + ... +} +``` + +Including a header file produces the same results as copying the header file +into each source file that needs it. Such copying would be time-consuming +and error-prone. With a header file, the related declarations appear +in only one place. If they need to be changed, they can be changed in one +place, and programs that include the header file will automatically use the +new version when next recompiled. The header file eliminates the labor of +finding and changing all the copies as well as the risk that a failure to +find one copy will result in inconsistencies within a program. + +In C, the usual convention is to give header files names that end with `.h'. +It is most portable to use only letters, digits, dashes, and underscores in +header file names, and at most one dot. + +Read more about using header files in official GCC documentation: + +* Include Syntax +* Include Operation +* Once-Only Headers +* Computed Includes + +https://gcc.gnu.org/onlinedocs/cpp/Header-Files.html diff --git a/lib/README b/lib/README new file mode 100644 index 0000000..6debab1 --- /dev/null +++ b/lib/README @@ -0,0 +1,46 @@ + +This directory is intended for project specific (private) libraries. +PlatformIO will compile them to static libraries and link into executable file. + +The source code of each library should be placed in a an own separate directory +("lib/your_library_name/[here are source files]"). + +For example, see a structure of the following two libraries `Foo` and `Bar`: + +|--lib +| | +| |--Bar +| | |--docs +| | |--examples +| | |--src +| | |- Bar.c +| | |- Bar.h +| | |- library.json (optional, custom build options, etc) https://docs.platformio.org/page/librarymanager/config.html +| | +| |--Foo +| | |- Foo.c +| | |- Foo.h +| | +| |- README --> THIS FILE +| +|- platformio.ini +|--src + |- main.c + +and a contents of `src/main.c`: +``` +#include +#include + +int main (void) +{ + ... +} + +``` + +PlatformIO Library Dependency Finder will find automatically dependent +libraries scanning project source files. + +More information about PlatformIO Library Dependency Finder +- https://docs.platformio.org/page/librarymanager/ldf.html diff --git a/platformio.ini b/platformio.ini new file mode 100644 index 0000000..8fdc791 --- /dev/null +++ b/platformio.ini @@ -0,0 +1,19 @@ +; PlatformIO Project Configuration File +; +; Build options: build flags, source filter +; Upload options: custom upload port, speed and extra flags +; Library options: dependencies, extra library storages +; Advanced options: extra scripting +; +; Please visit documentation for the other options and examples +; https://docs.platformio.org/page/projectconf.html + +[env:esp32doit-devkit-v1] +platform = espressif32 +board = esp32doit-devkit-v1 +framework = arduino +monitor_speed = 115200 +--upload-port = COM[14] +lib_deps = + bogde/HX711@^0.7.5 + mathertel/OneButton@^2.0.3 diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..aea38bf --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,178 @@ +#include "HX711.h" +#include "OneButton.h" + +// 硬件引脚定义-------------------------- +// 按钮 +#define BTN_UP 23 // 收线开关 接线:BTN_UP---GND +#define BTN_DOWN 22 // 放线开关 +#define BTN_CT 21 // 到顶检测开关 +// LED +#define LED_TIP 19 // 指示灯 接线:LED_TIP---3.3V +// 称重传感器- HX711 +#define LOADCELL_DOUT_PIN 2 +#define LOADCELL_SCK_PIN 4 + +// 角度传感器 +// 收放线电机控制 +// 控制串口直接使用Serial2,用法和Serial一样,如需要还可以用Serial1,但需要映射引脚 + +//--------------------------------- +OneButton button_up(BTN_UP, true); +OneButton button_down(BTN_DOWN, true); +OneButton button_checktop(BTN_CT, true); +HX711 scale; + +void upbtn_click(); +void downbtn_click(); +void ctbtn_pressend(); +void ctbtn_pressstart(); +void led_tip(bool onled); + +void setup() +{ + // 调试串口 + Serial.begin(115200); + Serial.println("Starting PullupDevice..."); + // 连接控制器 + Serial2.begin(115200); + // clear serialport + while (Serial2.read() >= 0) + { + } + + // 初始化按钮 + 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); // 抬起 + // 初始化称重传感器 + scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN); + scale.set_scale(516.f); // 这是缩放值,根据砝码实测 + scale.tare(); // 重置为0 + // 关闭灯光LED + pinMode(LED_TIP, OUTPUT); + led_tip(false); + + Serial.println("PullupDevice is ready!"); +} + +float Value; +#define FILTER_A 0.5 +// 低通滤波和限幅后的拉力数值,单位:克 +float Get_PullUnits() +{ + 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 Value; +} +// 提示灯光控制 +void led_tip(bool onled) +{ + if (onled) + digitalWrite(LED_TIP, LOW); // 亮灯 + else + digitalWrite(LED_TIP, HIGH); // 亮灯 +} + +void loop() +{ + button_checktop.tick(); + button_down.tick(); + button_up.tick(); + delay(10); + + // Serial.print("PullUnits:\t"); + // Serial.println(Get_PullUnits(), 1); +} + +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"); +} + +const int bufnum = 20; +char buffer[bufnum]; +const int sendbufnum = 5; +char sendbuffer[sendbufnum] = {(char)0xFF, (char)0xFF, (char)0x00, (char)0x00, (char)0x00}; +int datareadpos = 0; // 数据读取位置 +void getcommand() +{ + datareadpos = 0; + int headpos = 0; + + // 是否有数据,有就读bufnum个 + //数据头2个字节FF FF + while (Serial2.available() > 0) + { + buffer[datareadpos] = Serial2.read(); + + if ((datareadpos > 1) && (buffer[datareadpos] == 0xFF) && (buffer[datareadpos - 1] == 0xFF)) + headpos = datareadpos - 1; + datareadpos++; + delay(20); + // 读超过buff长度退出 + if (datareadpos >= bufnum) + break; + } + if (datareadpos > 0) + { + printf("rev[%d]:", datareadpos); + for (int i = 0; i < datareadpos; i++) + { + printf("0x%02X,", (byte)buffer[i]); + } + } + // 是否读了bufnum个数据 + if (((byte)buffer[headpos] == 0xFF) && ((byte)buffer[headpos + 1] == 0xFF)) + { + byte sumbyte = (byte)(buffer[headpos + 2] + buffer[headpos + 3] + buffer[headpos + 4]) & 0xFF; + if ((byte)buffer[headpos + 5] == sumbyte) + { + int cmd_type = buffer[headpos + 2]; + switch (cmd_type) + { + case 0x01: // 收线 + /* code */ + break; + case 0x02: // 放线 + /* code */ + break; + + default: + break; + } + } + } + // clear serial buffer + + while (Serial2.available() > 0) + { + Serial2.read(); + delay(5); + } + + for (int i = 0; i < bufnum; i++) + { + buffer[i] = '\0'; + } +} diff --git a/test/README b/test/README new file mode 100644 index 0000000..9b1e87b --- /dev/null +++ b/test/README @@ -0,0 +1,11 @@ + +This directory is intended for PlatformIO Test Runner and project tests. + +Unit Testing is a software testing method by which individual units of +source code, sets of one or more MCU program modules together with associated +control data, usage procedures, and operating procedures, are tested to +determine whether they are fit for use. Unit testing finds problems early +in the development cycle. + +More information about PlatformIO Unit Testing: +- https://docs.platformio.org/en/latest/advanced/unit-testing/index.html