You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
221 lines
6.8 KiB
221 lines
6.8 KiB
#include "device.hpp"
|
|
|
|
#include <stddef.h>
|
|
#include <stdio.h>
|
|
|
|
#define TAG "DEV"
|
|
namespace iflytop {
|
|
IflytopCanProtocolStackProcesser m_protocolStack;
|
|
|
|
TMC5130 m_motor1;
|
|
TMC5130 m_motor2;
|
|
|
|
ZGPIO debuglight;
|
|
|
|
ZGPIO triLight_R;
|
|
ZGPIO triLight_G;
|
|
ZGPIO triLight_B;
|
|
ZGPIO triLight_BEEP;
|
|
|
|
ZGPIO m_input1;
|
|
ZGPIO m_input2;
|
|
ZGPIO m_input3;
|
|
ZGPIO m_input4;
|
|
ZGPIO m_input5;
|
|
|
|
ZGPIO OUT_PD14;
|
|
ZGPIO OUT_PD15;
|
|
|
|
ZCanReceiver m_canReceiver;
|
|
ZCanBasicOrderModule m_basicOrderModule;
|
|
ZCanPumpCtrlModule m_pumpCtrlModule;
|
|
HuachengPressureSensor m_huachengPressureSensor;
|
|
PreportionalValveCtrl m_PreportionalValveHost;
|
|
CmdSchedulerV2 cmdScheduler;
|
|
|
|
void device_init() {
|
|
debuglight.initAsOutput(DEBUG_LIGHT_GPIO, ZGPIO::kMode_nopull, false, false);
|
|
ZHAL_CORE_REG(200, { debuglight.toggleState(); });
|
|
|
|
ZCanReceiver::CFG *cfg = m_canReceiver.createCFG(DEVICE_ID);
|
|
m_canReceiver.init(cfg);
|
|
// m_canReceiver.registerListener(this);
|
|
|
|
/**
|
|
* @brief 基础模块
|
|
*/
|
|
m_input1.initAsInput(PD11, ZGPIO::kMode_nopull, ZGPIO::kIRQ_noIrq, true /*mirror*/);
|
|
m_input2.initAsInput(PC5, ZGPIO::kMode_nopull, ZGPIO::kIRQ_noIrq, true /*mirror*/);
|
|
m_input3.initAsInput(PD12, ZGPIO::kMode_nopull, ZGPIO::kIRQ_noIrq, true /*mirror*/);
|
|
m_input4.initAsInput(PD13, ZGPIO::kMode_nopull, ZGPIO::kIRQ_noIrq, true /*mirror*/);
|
|
m_input5.initAsInput(PC6, ZGPIO::kMode_nopull, ZGPIO::kIRQ_noIrq, true /*mirror*/);
|
|
m_basicOrderModule.initialize(&m_canReceiver);
|
|
m_basicOrderModule.regInputCtl([](uint8_t id, bool &val) {
|
|
if (id == 1) {
|
|
val = m_input1.getState();
|
|
return true;
|
|
}
|
|
if (id == 2) {
|
|
val = m_input2.getState();
|
|
return true;
|
|
}
|
|
if (id == 3) {
|
|
val = m_input3.getState();
|
|
return true;
|
|
}
|
|
if (id == 4) {
|
|
val = m_input4.getState();
|
|
return true;
|
|
}
|
|
if (id == 5) {
|
|
val = m_input5.getState();
|
|
return true;
|
|
}
|
|
return false;
|
|
});
|
|
|
|
m_PreportionalValveHost.initialize(&huart2);
|
|
/*******************************************************************************
|
|
* 蠕动泵驱动 *
|
|
*******************************************************************************/
|
|
|
|
{
|
|
TMC5130::cfg_t cfg = {.hspi = &MOTOR_SPI, .enn_pin = MOTOR1_ENN, .csn_pin = MOTOR1_CSN};
|
|
|
|
m_motor1.initialize(&cfg);
|
|
int32_t chipv = m_motor1.readChipVERSION();
|
|
ZLOGI(TAG, "m_motor1:%lx", chipv);
|
|
m_motor1.setIHOLD_IRUN(1, 20, 0);
|
|
m_motor1.setMotorShaft(true);
|
|
|
|
m_motor1.setAcceleration(300000);
|
|
m_motor1.setDeceleration(300000);
|
|
// m_motor1.rotate(1000000);
|
|
}
|
|
|
|
{
|
|
TMC5130::cfg_t cfg = {.hspi = &MOTOR_SPI, .enn_pin = MOTOR2_ENN, .csn_pin = MOTOR2_CSN};
|
|
|
|
m_motor2.initialize(&cfg);
|
|
int32_t chipv = m_motor2.readChipVERSION();
|
|
ZLOGI(TAG, "m_motor2:%lx", chipv);
|
|
m_motor2.setIHOLD_IRUN(1, 20, 0); // 5W
|
|
m_motor2.setMotorShaft(true);
|
|
|
|
m_motor2.setAcceleration(300000);
|
|
m_motor2.setDeceleration(300000);
|
|
// m_motor1.rotate(1000000);
|
|
}
|
|
|
|
m_pumpCtrlModule.initialize(&m_canReceiver);
|
|
m_pumpCtrlModule.regSubmodule(1, [&](int16_t acc_rpm2, int16_t rpm, int16_t idlepower, int16_t power) {
|
|
ZLOGI(TAG, "pump1 acc_rpm2:%d rpm:%d", acc_rpm2, rpm);
|
|
setmotor(&m_motor1, acc_rpm2, rpm, idlepower, power);
|
|
});
|
|
m_pumpCtrlModule.regSubmodule(2, [&](int16_t acc_rpm2, int16_t rpm, int16_t idlepower, int16_t power) {
|
|
ZLOGI(TAG, "pump2 acc:%d rpm:%d", acc_rpm2, rpm);
|
|
setmotor(&m_motor2, acc_rpm2, rpm, idlepower, power);
|
|
});
|
|
|
|
/*******************************************************************************
|
|
* 三色指示灯 *
|
|
*******************************************************************************/
|
|
|
|
{
|
|
triLight_R.initAsOutput(PD8, ZGPIO::kMode_nopull, true, false);
|
|
triLight_G.initAsOutput(PD7, ZGPIO::kMode_nopull, true, false);
|
|
triLight_B.initAsOutput(PD9, ZGPIO::kMode_nopull, true, false);
|
|
triLight_BEEP.initAsOutput(PD10, ZGPIO::kMode_nopull, true, false);
|
|
}
|
|
|
|
/*******************************************************************************
|
|
* 压力传感器 *
|
|
*******************************************************************************/
|
|
|
|
{
|
|
m_huachengPressureSensor.initialize(&m_canReceiver);
|
|
m_huachengPressureSensor.regSubmodule(1, [](DP600PressureSensor::sensor_data_t *data) { //
|
|
static ModbusBlockHost modbusBlockHost;
|
|
modbusBlockHost.initialize(&huart3);
|
|
int16_t val[1] = {0};
|
|
bool suc = modbusBlockHost.readReg03Muti(1, 0x00, (uint16_t *)val, 1, 50);
|
|
if (!suc) {
|
|
return false;
|
|
}
|
|
data->precision = 3;
|
|
data->pressure_unit = 1;
|
|
data->value = val[0];
|
|
data->zero_point = 0;
|
|
data->range_full_point = 0;
|
|
return true;
|
|
});
|
|
m_huachengPressureSensor.regSubmodule(2, &huart3, 2);
|
|
m_huachengPressureSensor.regSubmodule(3, &huart3, 3);
|
|
m_huachengPressureSensor.regSubmodule(4, &huart3, 4);
|
|
}
|
|
|
|
OUT_PD14.initAsOutput(PD14, ZGPIO::kMode_nopull, false, true);
|
|
OUT_PD15.initAsOutput(PD15, ZGPIO::kMode_nopull, false, true);
|
|
}
|
|
|
|
void setmotor(TMC5130 *motor, int16_t acc_rpm2, int16_t rpm, int16_t idlepower, int16_t power) {
|
|
int32_t ppm = rpm / 60.0 * 51200;
|
|
int32_t acc = acc_rpm2 / 60.0 * 51200;
|
|
|
|
int16_t _idlepower = 1;
|
|
int16_t _power = 31;
|
|
|
|
if (idlepower > 0 && idlepower < 31) {
|
|
_idlepower = idlepower;
|
|
}
|
|
if (power > 0 && power < 31) {
|
|
_power = power;
|
|
}
|
|
|
|
motor->setIHOLD_IRUN(_idlepower, _power, 10); // 5W
|
|
motor->setAcceleration(acc);
|
|
motor->setDeceleration(acc);
|
|
motor->rotate(ppm);
|
|
}
|
|
|
|
void air_compressor_ch_select(int32_t val) {
|
|
if (val == 2) { // 内管路
|
|
OUT_PD15.setState(1);
|
|
} else if (val == 1) { // 空气
|
|
OUT_PD15.setState(0);
|
|
}
|
|
}
|
|
void air_compressor_valve1_set(int32_t val) { OUT_PD14.setState(val != 0); }
|
|
void air_compressor_valve2_set(int32_t val) { OUT_PD14.setState(val != 0); }
|
|
void air_compressor_read_pressure(int32_t *ack) { //
|
|
DP600PressureSensor::sensor_data_t d = m_huachengPressureSensor.readsensordata(2);
|
|
*ack = d.value;
|
|
}
|
|
|
|
void triple_warning_light_ctl(uint8_t r, uint8_t g, uint8_t b, uint8_t warning) {
|
|
triLight_R.setState(r != 0);
|
|
triLight_G.setState(g != 0);
|
|
triLight_B.setState(b != 0);
|
|
triLight_BEEP.setState(warning != 0);
|
|
}
|
|
|
|
int32_t preportional_valve_is_busy(int32_t *busy) {
|
|
int32_t valve1state = 0;
|
|
int32_t valve2state = 0;
|
|
int32_t err = 0;
|
|
*busy = 1;
|
|
|
|
err = m_PreportionalValveHost.isBusy(1, &valve1state);
|
|
if (err != 0) return err;
|
|
err = m_PreportionalValveHost.isBusy(2, &valve2state);
|
|
if (err != 0) return err;
|
|
|
|
if (valve1state == 0 && valve2state == 0) {
|
|
*busy = 0;
|
|
} else {
|
|
*busy = 1;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
} // namespace iflytop
|