|
|
#include "main.hpp"
#include <stddef.h>
#include <stdio.h>
#include "main.h"
#include "project.hpp"
//
// #include "sdk/components/single_axis_motor_control_v2/single_axis_motor_control_v2.hpp"
#include "sdk/components/iflytop_can_slave_modules/idcard_reader_service.hpp"
#include "sdk/components/single_axis_motor_control/single_axis_motor_control.hpp"
#include "sdk/hal/zhal.hpp"
#include "sdk\components\iflytop_can_slave_modules\io_control_service.hpp"
#include "sdk\components\iflytop_can_slave_v1\iflytop_can_slave.hpp"
#include "sdk\components\m3078\m3078_code_scaner.hpp"
#include "sdk\components\tmc\ic\ztmc4361A.hpp"
#include "sdk\components\tmc\ic\ztmc5130.hpp"
//
#include "sdk\components\huacheng_sensor\dp600_pressure_sensor.hpp"
#include "sdk\components\zcan_module\huacheng_pressure_sensor.hpp"
#include "sdk\components\zcan_module\zcan_basic_order_module.hpp"
#include "sdk\components\zcan_module\zcan_pump_ctrl_module.hpp"
#include "sdk\components\zcan_module\zcan_trigle_warning_light_ctl_module.hpp"
//
#include "adc.h"
#include "sdk\components\zcan_module\zcan_high_power_electrical_ctl_module.hpp"
#include "sdk\components\zcan_module\zcan_m211887_module_manager.hpp"
#define TAG "main"
namespace iflytop { Main gmain; };
using namespace iflytop;
IflytopCanProtocolStackProcesser m_protocolStack;
// TMC5130 m_motor1;
// TMC5130 m_motor2;
ZGPIO debuglight;
ZGPIO AirCompressorCtrl1; ZGPIO AirCompressorCtrl2;
ZGPIO AirBlowerCtrl1; ZGPIO AirBlowerCtrl2;
ZGPIO HeatingStripCtrl1; ZGPIO HeatingStripCtrl2;
ZGPIO output8; ZGPIO output9;
ZCanReceiver m_canReceiver; ZCanBasicOrderModule m_basicOrderModule; ZCanHighPowerElectricalCtlModule m_highPowerElectricalCtlModule; ZCanM211887ModuleManager m_m211887ModuleManager;
// ch(1-6Chnnal),times(读取次数)
uint32_t ADC_Get_Average(uint8_t ch, uint8_t times) { ADC_ChannelConfTypeDef sConfig; // 通道初始化
uint32_t value_sum = 0; uint8_t i; switch (ch) // 选择ADC通道
{ case 1: sConfig.Channel = ADC_CHANNEL_1; break; case 2: sConfig.Channel = ADC_CHANNEL_2; break; case 3: sConfig.Channel = ADC_CHANNEL_3; break; case 4: sConfig.Channel = ADC_CHANNEL_4; break; case 5: sConfig.Channel = ADC_CHANNEL_5; break; case 6: sConfig.Channel = ADC_CHANNEL_6; break; } sConfig.SamplingTime = ADC_SAMPLETIME_15CYCLES; // 采用周期239.5周期
sConfig.Rank = 1; HAL_ADC_ConfigChannel(&hadc1, &sConfig); for (i = 0; i < times; i++) { HAL_ADC_Start(&hadc1); // 启动转换
HAL_ADC_PollForConversion(&hadc1, 30); // 等待转化结束
value_sum += HAL_ADC_GetValue(&hadc1); // 求和
HAL_ADC_Stop(&hadc1); // 停止转换
} return value_sum / times; // 返回平均值
} void Main::onRceivePacket(CanPacketRxBuffer *rxbuf, uint8_t *packet, size_t len) { printf("can rx from %d %d,", rxbuf->id, len); for (size_t i = 0; i < len; i++) { printf("%02X ", packet[i]); } printf("\n"); }
void Main::run() { ZHALCORE::cfg_t oscfg = { .delayhtim = &DELAY_US_TIMER, .debuguart = &DEBUG_UART, }; ZHALCORE::getInstance()->initialize(oscfg);
ZLOGI(TAG, "zapp:%s", VERSION); 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);
AirCompressorCtrl1.initAsOutput(PC2, ZGPIO::kMode_nopull, true, false); AirCompressorCtrl2.initAsOutput(PC3, ZGPIO::kMode_nopull, true, false);
AirBlowerCtrl1.initAsOutput(PC4, ZGPIO::kMode_nopull, true, false); AirBlowerCtrl2.initAsOutput(PC5, ZGPIO::kMode_nopull, true, false);
HeatingStripCtrl1.initAsOutput(PC6, ZGPIO::kMode_nopull, true, false); HeatingStripCtrl2.initAsOutput(PC7, ZGPIO::kMode_nopull, true, false);
output8.initAsOutput(PD13, ZGPIO::kMode_nopull, true, false); output9.initAsOutput(PD14, ZGPIO::kMode_nopull, true, false);
// ZHAL_CORE_REG(3000, {
// AirCompressorCtrl1.toggleState();
// AirCompressorCtrl2.toggleState();
// AirBlowerCtrl1.toggleState();
// AirBlowerCtrl2.toggleState();
// HeatingStripCtrl1.toggleState();
// HeatingStripCtrl2.toggleState();
// });
/**
* @brief 基础模块 */ m_basicOrderModule.initialize(&m_canReceiver); m_basicOrderModule.regInputCtl([this](uint8_t id, bool &val) { return false; }); m_basicOrderModule.regOutCtl([this](uint8_t id, bool val) { ZLOGI(TAG, "set io %d %d", id, val); if (id == 0) { AirCompressorCtrl1.setState(val); return true; } if (id == 1) { AirCompressorCtrl2.setState(val); return true; } if (id == 2) { AirBlowerCtrl1.setState(val); return true; } if (id == 3) { AirBlowerCtrl2.setState(val); return true; } if (id == 4) { HeatingStripCtrl1.setState(val); return true; } if (id == 5) { HeatingStripCtrl2.setState(val); return true; }
if (id == 8) { output8.setState(val); return true; } if (id == 9) { output9.setState(val); return true; }
return false; });
m_basicOrderModule.regReadAdcVal([this](uint8_t id, int32_t &val) { if (id == 0) { val = ADC_Get_Average(0, 12); return true; } if (id == 1) { val = ADC_Get_Average(1, 12); return true; } if (id == 2) { val = ADC_Get_Average(2, 12); return true; } if (id == 3) { val = ADC_Get_Average(3, 12); return true; } if (id == 8) { val = ADC_Get_Average(8, 12); return true; } if (id == 9) { val = ADC_Get_Average(9, 12); return true; } return false; });
m_m211887ModuleManager.initialize(&m_canReceiver); m_m211887ModuleManager.regSubmodule(1, &huart3, 0xf0);
// while(true){
// uint8_t buf[10] ={1,2,3,4,5,6,7,8,9,10};
// HAL_UART_Transmit(&huart3,buf,10,1000);
// HAL_Delay(10);
// }
ZLOGI(TAG, "init done"); while (1) { ZHALCORE::getInstance()->loop(); } }
|