头文件如下:
#ifndef __SPEED_PR_H__
#define __SPEED_PR_H__
#define __SPEED_PR_H__
typedef struct {
Uint32 NewTimeStamp; // Variable : New 'Timestamp' corresponding to a capture event (Q0) - independently with global Q
Uint32 OldTimeStamp; // Variable : Old 'Timestamp' corresponding to a capture event (Q0) - independently with global Q
Uint32 TimeStamp; // Input : Current 'Timestamp' corresponding to a capture event (Q0) - independently with global Q
Uint32 SpeedScaler; // Parameter : Scaler converting 1/N cycles to a GLOBAL_Q speed (Q0) - independently with global Q
int32 EventPeriod; // Input/Variable : Event Period (Q0) - independently with global Q
int16 InputSelect; // Input : Input selection between TimeStamp (InputSelect=0) and EventPeriod (InputSelect=1)
_iq Speed; // Output : speed in per-unit
Uint32 BaseRpm; // Parameter : Scaler converting GLOBAL_Q speed to rpm (Q0) speed - independently with global Q
int32 SpeedRpm; // Output : speed in r.p.m. (Q0) - independently with global Q
void (*calc)(); // Pointer to the calulation function
} SPEED_MEAS_CAP; // Data type created
Uint32 NewTimeStamp; // Variable : New 'Timestamp' corresponding to a capture event (Q0) - independently with global Q
Uint32 OldTimeStamp; // Variable : Old 'Timestamp' corresponding to a capture event (Q0) - independently with global Q
Uint32 TimeStamp; // Input : Current 'Timestamp' corresponding to a capture event (Q0) - independently with global Q
Uint32 SpeedScaler; // Parameter : Scaler converting 1/N cycles to a GLOBAL_Q speed (Q0) - independently with global Q
int32 EventPeriod; // Input/Variable : Event Period (Q0) - independently with global Q
int16 InputSelect; // Input : Input selection between TimeStamp (InputSelect=0) and EventPeriod (InputSelect=1)
_iq Speed; // Output : speed in per-unit
Uint32 BaseRpm; // Parameter : Scaler converting GLOBAL_Q speed to rpm (Q0) speed - independently with global Q
int32 SpeedRpm; // Output : speed in r.p.m. (Q0) - independently with global Q
void (*calc)(); // Pointer to the calulation function
} SPEED_MEAS_CAP; // Data type created
/*
Note that
SpeedScaler = 60*f/rmp_max
where f = CLK_freq/(128*N), N = number of teeth in sprocket
and 128 is pre-determined prescaler of timer 2 (used by capture)
*/
Note that
SpeedScaler = 60*f/rmp_max
where f = CLK_freq/(128*N), N = number of teeth in sprocket
and 128 is pre-determined prescaler of timer 2 (used by capture)
*/
typedef SPEED_MEAS_CAP *SPEED_MEAS_CAP_handle;
/*-----------------------------------------------------------------------------
Default initalizer for the SPEED_MEAS_CAP object.
-----------------------------------------------------------------------------*/
// SpeedScaler = 260 based on f(clk) 25 MHz, f(samping)= 16.67, 25 kHz
// rmp_max = 1800 rpm
/*-----------------------------------------------------------------------------
Default initalizer for the SPEED_MEAS_CAP object.
-----------------------------------------------------------------------------*/
// SpeedScaler = 260 based on f(clk) 25 MHz, f(samping)= 16.67, 25 kHz
// rmp_max = 1800 rpm
#define SPEED_MEAS_CAP_DEFAULTS { 0, \
0, \
0, \
260, \
0, \
0, \
0, \
1800, \
0, \
(void (*)(Uint32))speed_prd_calc }
0, \
0, \
260, \
0, \
0, \
0, \
1800, \
0, \
(void (*)(Uint32))speed_prd_calc }
/*------------------------------------------------------------------------------
Prototypes for the functions in SPEED_PR.C
------------------------------------------------------------------------------*/
void speed_prd_calc(SPEED_MEAS_CAP_handle);
Prototypes for the functions in SPEED_PR.C
------------------------------------------------------------------------------*/
void speed_prd_calc(SPEED_MEAS_CAP_handle);
#endif // __SPEED_PR_H__
源文件如下:
#include "IQmathLib.h" // Include header for IQmath library
// Don't forget to set a proper GLOBAL_Q in "IQmathLib.h" file
#include "DSP281x_Device.h"
#include "speed_pr.h"
// Don't forget to set a proper GLOBAL_Q in "IQmathLib.h" file
#include "DSP281x_Device.h"
#include "speed_pr.h"
void speed_prd_calc(SPEED_MEAS_CAP *v)
{
if (v->InputSelect == 0)
{
v->OldTimeStamp = v->NewTimeStamp;
v->NewTimeStamp = v->TimeStamp;
v->EventPeriod = v->NewTimeStamp - v->OldTimeStamp;
if (v->EventPeriod < 0)
v->EventPeriod += 32767; // 0x7FFF = 32767
}
{
if (v->InputSelect == 0)
{
v->OldTimeStamp = v->NewTimeStamp;
v->NewTimeStamp = v->TimeStamp;
v->EventPeriod = v->NewTimeStamp - v->OldTimeStamp;
if (v->EventPeriod < 0)
v->EventPeriod += 32767; // 0x7FFF = 32767
}
v->Speed = _IQdiv(v->SpeedScaler,v->EventPeriod);
// Q0 = Q0*GLOBAL_Q => _IQXmpy(), X = GLOBAL_Q
v->SpeedRpm = _IQmpy(v->BaseRpm,v->Speed);
}
// Q0 = Q0*GLOBAL_Q => _IQXmpy(), X = GLOBAL_Q
v->SpeedRpm = _IQmpy(v->BaseRpm,v->Speed);
}
请问大佬们这是如何计算速度的?