YouSuanZhi/Core/Src/YouSuan.c
2026-03-17 10:42:36 +08:00

342 lines
9.4 KiB
C

#include "APPDEF.H"
#include "main.h"
#include "cmsis_os.h"
float ADCData;
float slope1,slope2;
float SetmL = 20;
float UseuL;
_Bool CuiQuFlag = false;
_Bool ZhongHeFlag = false;
uint16_t CCRV = 300;
float Blank = 0;
const uint8_t SampleCupMax = 8;
float ABSB; // 下吸光度
struct RunSet
{
uint16_t Speed_Stirrer; // 搅拌器速度
_Bool SampleCup[SampleCupMax]; // 样品杯是否存在
float AbsU; // 上吸光度
float ABSB; // 下吸光度
float SetmL; // 添加萃取液体积
float UseuL; // 中和液体积
float Sample_g; // 样品重量
_Bool RunFlag; // 启动标志位
};
float ABSBuf[200];
float UseuLBuf[200];
uint16_t BufIndex = 0;
uint32_t step = 100;
uint32_t ADCDataBuf[10];
extern void StirrerCtrl( uint16_t CCRV );
uint32_t AllStep = 51200;
float ADCRef = 0;
_Bool HasSample[8] = {false};
_Bool SetSample[8] = {true,true,true,true,true,true,true,true};
float WeigthSample[8];
float UseduL[8] = {0};
float mg_g[8] = {0};
float uL_Blank = 50;
_Bool SampleRunFlag = false;
float AverageData()
{
return (
(ADCDataBuf[0] >> 16) + (uint16_t)ADCDataBuf[0] +
(ADCDataBuf[1] >> 16) + (uint16_t)ADCDataBuf[1] +
(ADCDataBuf[2] >> 16) + (uint16_t)ADCDataBuf[2] +
(ADCDataBuf[3] >> 16) + (uint16_t)ADCDataBuf[3] ) / 8.0f;
}
const uint16_t SingelCupStep = 6400;
void HasSampleCheck()
{
SpeedSet[M3] = 10000;
CoordinateSet[M3] = - SingelCupStep /2;
do {
osDelay(200);
}
while( (CoordinateSet[M3] != CoordinatePosition[M3]) );
osDelay(100);
ADCData =AverageData();
ADCRef = Blank = ADCData;
CoordinateSet[M3] = AllStep;
uint8_t j = 0;
while( CoordinateSet[M3] != CoordinatePosition[M3])
{
ADCData =AverageData();
if( j < 8 )
{
if( CoordinatePosition[M3] > (- SingelCupStep / 4 + j * SingelCupStep - 850 )) // 正负850 共1700
{
if( CoordinatePosition[M3] < (- SingelCupStep / 4 + j * SingelCupStep + 850 ))
{
if( ADCRef - ADCData > 500 )
{
HasSample[j] = true;
j++;
}
}
else
{
HasSample[j] = false;
j++;
}
}
}
printf( "%2f,%3f\n",CoordinatePosition[M3] / 100.0f,ADCData);
osDelay(10);
}
do {
osDelay(200);
}
while( (CoordinateSet[M3] != CoordinatePosition[M3]) );
SpeedSet[M3] = 20000;
}
osThreadId_t SampleRunTaskHandle;
const osThreadAttr_t SampleRunTask_attributes = {
.name = "SampleRunTask",
.stack_size = 128 * 4,
.priority = (osPriority_t) osPriorityNormal,
};
#define ZHF 0.30f
#define DDF 0.27f
int16_t j = 0;
float dlteamin = 0.3f;
int16_t dlteaminCount = 0;;
void SampleRunTask( void *arg)
{
uint8_t i;
int count = 0;
if( !HasSample[0] )
{
vTaskDelete( NULL );
return;
}
for(i = 1; i < 8; i ++)
{
osDelay(1500);
if( HasSample[i] && SetSample[i])
{
CoordinateSet[M3] = i* SingelCupStep;
do {
osDelay(200);
}
while( (CoordinateSet[M3] != CoordinatePosition[M3]) );
osDelay(1500);
Typestr("%d号杯萃取",i);
CoordinateSet[M2] = SetmL *slope1;
CuiQuFlag = false;
StirrerCtrl(CCRV);
do {
osDelay(200);
}
while( (CoordinateSet[M2] != CoordinatePosition[M2]) );
StirrerCtrl(0);
osDelay(1500);
CoordinateSet[M3] = 0;
do {
osDelay(200);
}
while( (CoordinateSet[M3] != CoordinatePosition[M3]) );
osDelay(1000);
SpeedSet[0] = 1000;
CoordinateSet[M1] = 1000/slope2;
do {
osDelay(200);
}
while( (CoordinateSet[M1] != CoordinatePosition[M1]) );
SpeedSet[0] = 20;
osDelay(1000);
CoordinateSet[M3] = i* SingelCupStep;
do {
osDelay(200);
}
while( (CoordinateSet[M3] != CoordinatePosition[M3]) );
osDelay(1500);
BufIndex = 0;
UseuL = 0;
Typestr("%d号杯中和",i);
ADCData =AverageData();
Blank = ADCData;
CoordinateSet[M1] = 6550000;
StirrerCtrl(CCRV);
do {
ADCData = AverageData();
ABSB = (uint16_t)(log10(Blank/ADCData) *1000 ) / 1000.0f;
UseuL = (uint32_t)(PluseCount[M1] * slope2 * 1000) / 1000.0f;
ABSBuf[BufIndex] = ABSB;
UseuLBuf[BufIndex] = UseuL;
pfp( "%.3f,%3f\n",ABSB,ADCData);
if(ABSB > ZHF)
{
count ++;
if( count >= 5)
{
dlteamin = ZHF;
CoordinateSet[M1] = 0;
ZhongHeFlag = false;
StirrerCtrl(100);
osDelay(1000);
j = BufIndex;
while( j > 0)
{
j--;
if( fabs(ABSBuf[j] - DDF) < dlteamin )
{
dlteamin = fabs(ABSBuf[j] - DDF);
dlteaminCount = j;
}
if( ABSBuf[j] < DDF )
{
UseduL[i] = UseuLBuf[j];
j = -1;
break;
}
}
if( j == 0 )
{
for( j = 199; j > BufIndex; j -- )
{
if( fabs(ABSBuf[j] - DDF) < dlteamin )
{
dlteamin = fabs(ABSBuf[j] - DDF);
dlteaminCount = j;
}
if( ABSBuf[j] < DDF )
{
UseduL[i] = UseuLBuf[j];
j = -1;
break;
}
}
if( j == BufIndex )
{
if( fabs(ABSBuf[j] - DDF) < dlteamin )
{
dlteamin = fabs(ABSBuf[j] - DDF);
dlteaminCount = j;
}
UseduL[i] = UseuLBuf[dlteaminCount];
//没有小于0.27的数则取最接近的数
}
}
}
}
else
{
count = 0;
}
if(++BufIndex >=200)
BufIndex = 0;
osDelay(50);
} while( CoordinateSet[M1] != CoordinatePosition[M1] );
if(WeigthSample[i]!= 0)
{
mg_g[i] = (int32_t)((UseduL[i] - uL_Blank)*0.02*56.1/WeigthSample[i] * 1000 ) /1000000.0f;
}
else
{
uL_Blank = UseduL[i];
}
}
else
{
Typestr("空位");
CoordinateSet[M1] = 0;
}
}
StirrerCtrl(0);
osDelay(1500);
CoordinateSet[M3] = 0;
do {
osDelay(200);
}
while( (CoordinateSet[M3] != CoordinatePosition[M3]) );
osDelay(1000);
vTaskDelete( NULL );
}
_Bool FillFlag = false;
void YouSuanRun( void )
{
Typestr("进入系统" );
MODBUS_Init(1);
Debuglog("");
slope1 = 82700;
slope2 = 0.01166;
MotorCTRLInitiate();
Debuglog("");
CoordinateSet[M1] = 0;
CoordinateSet[M2] = 0;
CoordinateSet[M3] = 0;
MotorZero_Init(M3);
Debuglog("");
HasSampleCheck();
Debuglog("n");
SpeedSet[0] = 20;
SpeedSet[0] = 1000;
SpeedSet[3] = 5000;
ADCData =AverageData();
Blank = ADCData;
for(;;)
{
if(FillFlag)
{
FillFlag = 0;
CoordinateSet[M3] = 0;
do {
osDelay(200);
}
while( (CoordinateSet[M3] != CoordinatePosition[M3]) );
CoordinateSet[M2] = 1 *slope1;
CuiQuFlag = false;
do {
osDelay(200);
}
while( (CoordinateSet[M2] != CoordinatePosition[M2]) );
SpeedSet[0] = 1000;
CoordinateSet[M1] = 1000 / slope2;
do {
osDelay(200);
}
while( (CoordinateSet[M1] != CoordinatePosition[M1]) );
SpeedSet[0] = 20;
}
if(SampleRunFlag)
{
SampleRunTaskHandle = osThreadNew( SampleRunTask, NULL, &SampleRunTask_attributes);
SampleRunFlag = false;
}
else
{
if( FindZero[M3] )
{
Typestr("找零")
Debuglog("");
MotorZero_Init(M3);
Debuglog("");
}
}
osDelay(200);
}
}