RobotHardware-UESTC/Hardware/银星机器人底盘/PiRobot-YH_Firmware v1.0/STM32/Lib/Encoder/encoder_implement.cpp

53 lines
1.1 KiB
C++
Raw Normal View History

2024-01-20 13:19:09 +08:00
#include "encoder_implement.h"
#include "board.h"
#include "print.h"
EncoderImp::EncoderImp(unsigned char _num, bool _reverse) : reverse(_reverse), num(_num)
{
clear();
}
void EncoderImp::init()
{
Board::get()->encoder_init(num);
clear();
}
void EncoderImp::clear()
{
total_count = 0;
pid_pos = odom_pos = 0;
}
long EncoderImp::get_total_count()
{
if (reverse)
2024-01-24 22:08:32 +08:00
total_count += Board::get()->GetEncoderCount(num);
2024-01-20 13:19:09 +08:00
else
2024-01-24 22:08:32 +08:00
total_count -= Board::get()->GetEncoderCount(num);
2024-01-20 13:19:09 +08:00
return total_count;
}
long EncoderImp::get_increment_count_for_dopid()
{
if (reverse)
2024-01-24 22:08:32 +08:00
total_count += Board::get()->GetEncoderCount(num);
2024-01-20 13:19:09 +08:00
else
2024-01-24 22:08:32 +08:00
total_count -= Board::get()->GetEncoderCount(num);
2024-01-20 13:19:09 +08:00
long l = total_count-pid_pos;
pid_pos = total_count;
return l;
}
long EncoderImp::get_increment_count_for_odom()
{
if (reverse)
2024-01-24 22:08:32 +08:00
total_count += Board::get()->GetEncoderCount(num);
2024-01-20 13:19:09 +08:00
else
2024-01-24 22:08:32 +08:00
total_count -= Board::get()->GetEncoderCount(num);
2024-01-20 13:19:09 +08:00
long l = total_count-odom_pos;
odom_pos = total_count;
return l;
}