polarimeter_software/User/app/pm_common.c
2025-09-30 10:37:23 +08:00

91 lines
2.6 KiB
C
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
* @Author: mypx
* @Date: 2025-07-18 08:53:22
* @LastEditTime: 2025-09-24 14:24:19
* @LastEditors: mypx mypx_coder@163.com
* @Description:
*/
#include "pm_common.h"
#include "et_encoder_utils.h"
#include "et_log.h"
struct rt_event pm_event;
uint32_t encoder_cpr;
/**
* @brief Correct specific rotation for temperature using linear coefficient
*
* @param specific_rotation_ref Specific rotation at reference temperature (°)
* @param tref Reference temperature (°C)
* @param temp Target temperature (°C)
* @param temp_coeff Temperature coefficient (° per °C)
* @return Corrected specific rotation at target temperature (°)
*/
float pm_correct_specific_rotation_temperature(float specific_rotation_ref, float tref, float temp, float temp_coeff)
{
/* Simple linear correction: [α](T) = [α](Tref) + coeff * (T - Tref) */
return specific_rotation_ref + temp_coeff * (temp - tref);
}
/**
* @brief Convert concentration from g/100mL to g/mL
*/
float pm_convert_g_per_100ml_to_g_per_ml(float conc_g_per_100ml)
{
return conc_g_per_100ml / 100.0f; /* g/100mL -> g/mL */
}
/**
* @brief Convert concentration from g/100mL to mol/L
*/
float pm_convert_g_per_100ml_to_molar(float conc_g_per_100ml, float molar_mass_g_per_mol)
{
/* g/100mL -> g/L: multiply by 10 */
float g_per_l = conc_g_per_100ml * 10.0f;
if (molar_mass_g_per_mol == 0.0f)
{
ET_ERR("molar mass cannot be zero\n");
return NAN;
}
return g_per_l / molar_mass_g_per_mol; /* mol/L */
}
float pm_deg_to_rad(float deg)
{
return deg * (3.14159265358979323846f / 180.0f);
}
float pm_rad_to_deg(float rad)
{
return rad * (180.0f / 3.14159265358979323846f);
}
static double calculate_real_time_angle_degree(int64_t encoder_cnt_total)
{
return (double)(encoder_cnt_total) * 360.0f / encoder_cpr;
}
double get_real_time_angle_degree(void)
{
int64_t total_encoder_cnt = 0;
int32_t curent_encoder_cnt = 0;
curent_encoder_cnt = pm_encoder_get_count();
//curent_encoder_cnt = 65535 - curent_encoder_cnt; // 反向计数,或者更改AB线序
total_encoder_cnt = et_encoder_get_whole_count(curent_encoder_cnt, &encoder_tim_overflow_count,
ENCODER_MAX_COUNT);
//ET_DBG("encoder_cnt: %lld\n", (long long)total_encoder_cnt);
return calculate_real_time_angle_degree(total_encoder_cnt);
}
rt_uint32_t convert_encoder_angle_rpm_to_to_time(double angle, float m_rpm)
{
rt_uint32_t delay = 0;
double m_r = angle * MOTOR_TO_ENCODER_RATIO / 360.0;
delay = (fabs(m_r) * 60.0 * 1000 / fabs(m_rpm)); // ms
return delay;
}