179 lines
4.2 KiB
C++
179 lines
4.2 KiB
C++
|
#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';
|
|||
|
}
|
|||
|
}
|