初次提交

This commit is contained in:
pxzleo 2023-04-11 19:16:14 +08:00
commit 818afa73a1
7 changed files with 308 additions and 0 deletions

5
.gitignore vendored Normal file
View File

@ -0,0 +1,5 @@
.pio
.vscode/.browse.c_cpp.db*
.vscode/c_cpp_properties.json
.vscode/launch.json
.vscode/ipch

10
.vscode/extensions.json vendored Normal file
View File

@ -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"
]
}

39
include/README Normal file
View File

@ -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

46
lib/README Normal file
View File

@ -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 <Foo.h>
#include <Bar.h>
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

19
platformio.ini Normal file
View File

@ -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

178
src/main.cpp Normal file
View File

@ -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';
}
}

11
test/README Normal file
View File

@ -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