void user_adc_task( void* unused );
-ALIGN_32BYTES (static uint16_t adc_raw[3][512] );
-static uint16_t adc_raw_copy[3][512];
-static uint32_t adc_update_flag[3];
+//ADC1 INP5压力计 INP9流量计 10毫秒采集一次,100毫秒DMA,取平均计算
+//ADC3 INP2温度 10毫秒采集一次,100毫秒,取平均计算。
+//ADC2 INP18 电流, 100微秒采集一次,500毫秒,取平均计算。
+
+ALIGN_32BYTES (static uint16_t adc1_raw[10*2] ); //10毫米采集
+ALIGN_32BYTES (static uint16_t adc2_raw[5000] ); //电流采样率10Khz
+ALIGN_32BYTES (static uint16_t adc3_raw[10] ); //采样率 20Hz
+
+static uint16_t adc1_raw_copy[10*2];
+static uint16_t adc3_raw_copy[10];
+static float adc2_curr[5000];
extern dev_info_t g_dev;
void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc)
{
if( hadc == &hadc1 ){
- SCB_InvalidateDCache_by_Addr((uint32_t *) &adc_raw[0], 1024);
- memcpy( adc_raw_copy[0], adc_raw[0], 500*2 );
- adc_update_flag[0] = 1;
+ SCB_InvalidateDCache_by_Addr((uint32_t *)&adc1_raw[0], sizeof( adc1_raw) );
+ memcpy( &adc1_raw_copy[0], &adc1_raw[0], sizeof( adc1_raw) );
+ adc1_update_flag = 1;
+ osEventFlagsSet( g_dev.ev_group, EVENT_ADC1_UPDATE );
}else if( hadc == &hadc2 ){
- SCB_InvalidateDCache_by_Addr((uint32_t *) &adc_raw[1], 1024);
- memcpy( adc_raw_copy[1], adc_raw[1], 500*2 );
- adc_update_flag[1] = 1;
+ SCB_InvalidateDCache_by_Addr((uint32_t *)&adc2_raw[0], sizeof( adc2_raw) );
+ for( int32_t i=1; i< sizeof( adc2_raw)/2; i++ )
+ adc2_curr[i] = (adc2_raw[i] * VREF*2/0xffff - 2.5)/0.625*600;
+ adc2_update_flag = 1;
+ osEventFlagsSet( g_dev.ev_group, EVENT_ADC2_UPDATE );
}else if( hadc == &hadc3 ){
- SCB_InvalidateDCache_by_Addr((uint32_t *) &adc_raw[2], 1024);
- memcpy( adc_raw_copy[2], adc_raw[2], 500*2 );
- adc_update_flag[2] = 1;
+ SCB_InvalidateDCache_by_Addr((uint32_t *)&adc3_raw[0], sizeof( adc3_raw) );
+ memcpy( &adc3_raw_copy[0], &adc3_raw[0], sizeof( adc3_raw) );
+ adc3_update_flag = 1;
+ osEventFlagsSet( g_dev.ev_group, EVENT_ADC3_UPDATE );
}
}
return s/len;
}
-#if 1
//#define VREF 2.5f
static float welding_curr_raw[500];
-float calc_welding_current( uint16_t *values, uint16_t len )
+float calc_welding_current( float *values, uint32_t len )
{
//计算焊接电流
//1. 计算平均值
float welding_curr = 0;
for( int i=0; i<len; i++ ){
- curr = (values[i]* VREF*2/0xffff - 2.5)/0.625*400;
+ //curr = (values[i]* VREF*6/0xffff - 2.5)/0.625*600;
+ curr = values[i];
welding_curr_raw[i] = abs( curr );
s_curr += welding_curr_raw[i];
if( curr < -5 ){ //低于-5A 判断为负电流,避免0点附近抖动影响
}
}
- if( m_nr > 5 && p_nr > 5 && m_nr > z_nr && p_nr > z_nr ){
- //交流, 采集500次,其中有5次以上正和5次以上负电流,判断为交流电
+ if( m_nr > 50 && p_nr > 50 && m_nr > z_nr && p_nr > z_nr ){
+ //交流, 采集5000次,其中有50次以上正和50次以上负电流,判断为交流电
float s = 0;
for( int i=0; i<len; i++ ){
s += welding_curr_raw[i]*welding_curr_raw[i];
}
return welding_curr;
}
-#endif
float vol_to_kpa( float vol )
{
return (vol-0.2f)*1000/6.4f;
}
+float vol_to_speed( float vol )
+{
+ //AFM07系列输出电压与瞬时流量的关系为:电压(V) = 1+瞬时流量(L/min)×电压系数。
+ //AFM07系列有25L/min和50L/min两个量程,如表3所示,两个量程对应的电压系数分别为0.16和0.08。
+ float speed = 0;
+ if( vol <= 1.0f )
+ speed = 0;
+ else if( vol >= 5.0f )
+ speed = 50;
+ else
+ speed = (vol -1.0f)/0.08f; //最小 1V -- 0L/min, 最大 5v - - 50L/min
+ return speed;
+}
+
void user_adc_task( void* unused )
{
uint32_t raw;
uint32_t curr_err = 0;
uint32_t temp_err = 0;
+ uint32_t pressure_sum = 0;
+ uint32_t flowrate_sum = 0;
+
+ EventBits_t uxBits;
+
float dbg_curr; //for debug
HAL_ADCEx_Calibration_Start( &hadc1, ADC_CALIB_OFFSET_LINEARITY, ADC_SINGLE_ENDED );
HAL_ADCEx_Calibration_Start( &hadc3, ADC_CALIB_OFFSET_LINEARITY, ADC_SINGLE_ENDED );
osDelay(100);
+ HAL_TIM_Base_Start_IT( &htim1);
HAL_TIM_Base_Start_IT( &htim2);
- //ADC 用TIM2 1毫秒触发采集,dma采集500次0.5秒产生中断
- HAL_ADC_Start_DMA( &hadc1, (uint32_t*)adc_raw[0], 500 );
- HAL_ADC_Start_DMA( &hadc2, (uint32_t*)adc_raw[1], 500 );
- HAL_ADC_Start_DMA( &hadc3, (uint32_t*)adc_raw[2], 500 );
+ //ADC1 用TIM2 10毫秒触发采集,dma采集10次0.1秒产生中断
+ //ADC2 用TIM1 100微秒触发采集,dma采集5000次0.5秒产生中断
+ //ADC3 用TIM2 10毫秒触发采集,dma采集10次0.1秒产生中断
+ HAL_ADC_Start_DMA( &hadc1, (uint32_t*)&adc1_raw[0], sizeof( adc1_raw)/sizeof( adc1_raw[0]) );
+ HAL_ADC_Start_DMA( &hadc2, (uint32_t*)&adc2_raw[0], sizeof( adc2_raw)/sizeof( adc2_raw[0]) );
+ HAL_ADC_Start_DMA( &hadc3, (uint32_t*)&adc3_raw[0], sizeof( adc3_raw)/sizeof( adc3_raw[0]) );
- adc_update_flag[0] = 0;
- adc_update_flag[1] = 0;
- adc_update_flag[2] = 0;
uint32_t last_tick = xTaskGetTickCount();
while(1){
- //10毫秒扫描一次
- //TODO 改成event出发,ADC采集完成取触发,
- vTaskDelayUntil(&last_tick, 10 );
+ const TickType_t xTicksToWait = 100000 / portTICK_PERIOD_MS;
+ uxBits = xEventGroupWaitBits(
+ g_dev.ev_group, /* The event group being tested. */
+ EVENT_ADC1_UPDATE|EVENT_ADC2_UPDATE|EVENT_ADC3_UPDATE, /* The bits within the event group to wait for. */
+ pdTRUE, /* BIT_x should be cleared before returning. */
+ pdFALSE, /* Don't wait for all bits, either bit will do. */
+ xTicksToWait );/* Wait a maximum of xxx ms for either bit to be set. */
+
+ if( uxBits & EVENT_ADC1_UPDATE ){
+ // ADC1 压力计 和 流量计
+ HAL_ADC_Start_DMA( &hadc1, (uint32_t*)&adc1_raw[0], sizeof(adc1_raw)/sizeof(adc1_raw[0]) );
+
+ pressure_sum = 0;
+ flowrate_sum = 0;
- if( adc_update_flag[0] ){
- //压力计 ADC1
- adc_update_flag[0] = 0;
- HAL_ADC_Start_DMA( &hadc1, (uint32_t*)adc_raw[0], 500 );
- raw = adc_average( adc_raw_copy[0], 500 );
+ for( int i=0; i<sizeof(adc1_raw)/sizeof(adc1_raw[0])/2; i++ ){
+ pressure_sum += adc1_raw[ i*2 ];
+ flowrate_sum += adc1_raw[ i*2+1 ];
+ }
+
+ raw = pressure_sum/(sizeof( adc1_raw)/sizeof(adc1_raw[0])/2);
voltage = raw*VREF/0xffff; //16bit adc1
voltage *=2; //压力计输出电压 分压 1/2
- g_dev.pressure = vol_to_kpa( voltage );
-
+ g_dev.pressure = vol_to_kpa( voltage );
+ g_dev.flowmeter.flow_speed_ad = vol_to_speed( flowrate_sum/(sizeof(adc1_raw)/sizeof(adc1_raw[0])/2) *VREF/0xffff *2 );
+
if( voltage < 0.2f ){
//传感器输出0.2V到4.7v,未通气时按一个大气压101KPa大约0.2 + 101*0.0064 = 0.85V
if( pressure_err > 10 ){
g_dev.state &= ~PRESSURE_SENSOR_ERR;
}
}
- }
-
- if( adc_update_flag[1] ){
+ }
+ if( uxBits & EVENT_ADC2_UPDATE ){
//电流互感器 ADC2
- adc_update_flag[1] = 0;
- HAL_ADC_Start_DMA( &hadc2, (uint32_t*)adc_raw[1], 500 );
-
- if(0){
- //TODO:如果脉冲模式,改成查找峰值
- }else{
- //非脉冲模式,取平均值
- raw = adc_average( adc_raw_copy[1], 500 );
- voltage = raw*VREF/0xffff; //16bit adc2
- g_dev.welding_current_raw = raw;
- }
- {
- extern uint32_t dbg_report_raw_current;
- extern uint8_t CDC_Transmit_HS(uint8_t* Buf, uint16_t Len);
-
- static uint8_t dbg_curr_raw[1024] = {0xA5, 0x5A, 0x00, 0x04, };
- if( dbg_report_raw_current ){
- memcpy( &dbg_curr_raw[4], adc_raw_copy[1], 1000 );
- CDC_Transmit_HS( (uint8_t*)dbg_curr_raw, 1024 );
+ HAL_ADC_Start_DMA( &hadc2, (uint32_t*)&adc2_raw[0], sizeof(adc2_raw)/sizeof(adc2_raw[0]) );
+ { //发送调试数据到pc,
+ extern uint32_t dbg_report_raw_current;
+ extern uint8_t CDC_Transmit_HS(uint8_t* Buf, uint16_t Len);
+ static uint8_t dbg_curr_raw[sizeof(adc2_curr)/sizeof(adc2_curr[0])*2+4] = {0xA5, 0x5A, (sizeof(adc2_curr)/sizeof(adc2_curr[0]))&0xff, ((sizeof(adc2_curr)/sizeof(adc2_curr[0]))>>8)&0xff, };
+ if( dbg_report_raw_current ){
+ for( int i=0; i < sizeof(adc2_curr)/sizeof(adc2_curr[0]); i++ ){
+ int16_t curr = adc2_curr[i];
+ dbg_curr_raw[4+i*2] = curr&0xff;
+ dbg_curr_raw[4+i*2+1] = (curr>>8)&0xff;
+ }
+ CDC_Transmit_HS( (uint8_t*)dbg_curr_raw, 10004 );
+ }
}
- }
- //计算公式:2.5V±0.625V 0V基准
- //正向:2.5+(I/Ipn)*0.625
- //反向:2.5-(I/Ipn)*0.625
- //额定输入 Ipn = 400A, 测量范围 Ipm = Ipn*1.2
- //电流 = (V-2.5)/0.625 * Ipn
-
- voltage *=2; //互感器输出电压 分压 1/2
-
+ //计算公式:2.5V±0.625V 0V基准, //正向:2.5+(I/Ipn)*0.625, //反向:2.5-(I/Ipn)*0.625
+ //额定输入 Ipn = 600A, 测量范围 Ipm = Ipn*1.2, //电流 = (V-2.5)/0.625 * Ipn
if( dbg_current_read( &dbg_curr )==0 ){
//模拟电流用于调试
g_dev.welding_current = dbg_curr;
}else{
- g_dev.welding_current = calc_welding_current( adc_raw_copy[1], 500 );
+ g_dev.welding_current = calc_welding_current( adc2_curr, sizeof(adc2_curr)/sizeof(adc2_curr[0]) );
}
- if( voltage < 1.5f ){
+ if( g_dev.welding_current < -500 ){
//传感器输出范围 2.5-0.65 到2.5+0.65
if( curr_err > 10 ){
//0.5秒*10=5秒后报告传感器故障
g_dev.state &= ~CURR_SENSOR_ERR;
}
}
- }
-
- if( adc_update_flag[2] ){
+ }
+ if( uxBits & EVENT_ADC3_UPDATE ){
//比例阀温度侦测 ADC3
/*
<0.55V告警锁存
-----------------------------------------------------------------------------------
*/
- adc_update_flag[2] = 0;
- HAL_ADC_Start_DMA( &hadc3, (uint32_t*)adc_raw[2], 500 );
+ HAL_ADC_Start_DMA( &hadc3, (uint32_t*)&adc3_raw[0], sizeof(adc3_raw)/sizeof(adc3_raw[0]) );
- raw = adc_average( adc_raw_copy[2], 500 );
+ raw = adc_average( adc3_raw_copy, sizeof(adc3_raw_copy)/sizeof(adc3_raw_copy[0]) );
voltage = raw*VREF/0x0fff; //12bit adc3
g_dev.valve_temperature = voltage;