miniFOC 1.0.3
This open-source project aims to accomplish a FOC(Field Oriented Control) scheme that is operatable with minimum costs in China.
Functions | Variables
encoder.c File Reference

this file contains the code implementation of angle acquisition and initialization functions of sc60228 and sc60224. More...

#include "encoder.h"
#include "spi.h"
#include "gd32f1x0.h"
#include "foc.h"
#include "timer.h"
#include "system.h"
#include "config.h"
#include "filter.h"

Go to the source code of this file.

Functions

void encoder_delay (void)
 delay function for magnetic encoder More...
 
unsigned short encoder_read_data (unsigned short TxData)
 read data from the register of the magnetic encoder More...
 
unsigned short encoder_get_mechanical_angle (void)
 read mechanical angle directly from encoder More...
 
float encoder_get_electronic_angle (void)
 according to the electrical angle calculated from the mechanical angle, this function will call encoder_get_mechanical_angle() function. More...
 
void encoder_update_speed (void)
 called every 2 milliseconds to calculate the speed. More...
 
void encoder_zeroing (void)
 correct the mechanical angle zero deviation between the magnetic encoder and FOC. More...
 

Variables

volatile unsigned short machine_angle_offset = 0
 mechanical angle offset, which is used to align the mechanical angle with the zero point of the electrical angle. More...
 
static volatile unsigned short last_mechanical_angle = 0
 the mechanical angle at the last moment is used to calculate the angle differential value and realize the angle measurement function. More...
 
static volatile long long total_machine_angle = 0
 the total mechanical angle since power on is used to calculate the angle integral value and realize the angle measurement function. More...
 
static volatile long long systick_mechanical_angle_last = 0
 the mechanical angle at the last moment is used to calculate the rotation speed of the motor rotor. More...
 

Detailed Description

this file contains the code implementation of angle acquisition and initialization functions of sc60228 and sc60224.

Author
Lao·Zhu
Version
V1.0.2
Date
29. October 2021

Definition in file encoder.c.

Function Documentation

◆ encoder_delay()

void encoder_delay ( void  )

delay function for magnetic encoder

Definition at line 44 of file encoder.c.

Referenced by encoder_read_data().

◆ encoder_get_electronic_angle()

float encoder_get_electronic_angle ( void  )

according to the electrical angle calculated from the mechanical angle, this function will call encoder_get_mechanical_angle() function.

Return values
registerraw data reading back

Definition at line 93 of file encoder.c.

References ELECANGLE_COEFFICIENT, encoder_get_mechanical_angle(), ENCODER_RESO, FOC_Struct, MECHANGLE_COEFFICIENT, FOC_Structure_t::mechanical_angle, and POLAR_PAIRS.

Referenced by TIMER2_IRQHandler().

◆ encoder_get_mechanical_angle()

unsigned short encoder_get_mechanical_angle ( void  )

read mechanical angle directly from encoder

Return values
registerraw data reading back

Definition at line 76 of file encoder.c.

References encoder_read_data(), ENCODER_RESO, last_mechanical_angle, machine_angle_offset, and total_machine_angle.

Referenced by encoder_get_electronic_angle(), and foc_calibrate_phase().

◆ encoder_read_data()

unsigned short encoder_read_data ( unsigned short  TxData)

read data from the register of the magnetic encoder

Parameters
[in]TxDatadata to be sent to magnetic encoder
Return values
dataread from magnetic encoder

Definition at line 56 of file encoder.c.

References encoder_delay(), and spi_readwrite_halfworld().

Referenced by encoder_get_mechanical_angle(), and encoder_zeroing().

◆ encoder_update_speed()

void encoder_update_speed ( void  )

called every 2 milliseconds to calculate the speed.

Definition at line 106 of file encoder.c.

References filter_update_value(), FOC_Struct, FOC_Structure_t::rotate_speed, systick_mechanical_angle_last, total_machine_angle, and velocity_filter.

Referenced by SysTick_Handler().

◆ encoder_zeroing()

void encoder_zeroing ( void  )

correct the mechanical angle zero deviation between the magnetic encoder and FOC.

Definition at line 119 of file encoder.c.

References CALI_TORQUE, delayms(), encoder_read_data(), foc_calculate_dutycycle(), machine_angle_offset, total_machine_angle, and update_pwm_dutycycle().

Referenced by main().

Variable Documentation

◆ last_mechanical_angle

volatile unsigned short last_mechanical_angle = 0
static

the mechanical angle at the last moment is used to calculate the angle differential value and realize the angle measurement function.

Definition at line 29 of file encoder.c.

Referenced by encoder_get_mechanical_angle().

◆ machine_angle_offset

volatile unsigned short machine_angle_offset = 0

mechanical angle offset, which is used to align the mechanical angle with the zero point of the electrical angle.

Definition at line 23 of file encoder.c.

Referenced by encoder_get_mechanical_angle(), encoder_zeroing(), flash_read_parameters(), flash_write_parameters(), and report_local_variable().

◆ systick_mechanical_angle_last

volatile long long systick_mechanical_angle_last = 0
static

the mechanical angle at the last moment is used to calculate the rotation speed of the motor rotor.

Definition at line 39 of file encoder.c.

Referenced by encoder_update_speed().

◆ total_machine_angle

volatile long long total_machine_angle = 0
static

the total mechanical angle since power on is used to calculate the angle integral value and realize the angle measurement function.

Definition at line 34 of file encoder.c.

Referenced by encoder_get_mechanical_angle(), encoder_update_speed(), and encoder_zeroing().