MotorControlModuleSDFM_TMS3.../Projects/EFC_Communication/UMLibrary/driver/BinaryEncoderSync.hh
2024-06-07 11:12:56 +03:00

84 lines
2.1 KiB
C++
Raw 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.

/*
* BinaryEncoderSync.hh
*
* Created on: 19 янв. 2022 г.
* Author: titov
*/
#ifndef UMLIBRARY_DRIVER_BINARYENCODERSYNC_HH_
#define UMLIBRARY_DRIVER_BINARYENCODERSYNC_HH_
#include <atomic>
#include <cmath>
#include <stdint.h>
#include "IEncoder.hh"
#include "../systemic/IProcess.hh"
#include "../common/DoubleBuffer.hpp"
#include "../communication/IBinaryDataSubscriber.hh"
#include "../communication/format/BinaryHelpers.hh"
#include "../peripheral/NanoTimer.hh"
namespace driver { namespace detail {
class BinaryEncoderSync : public IEncoder, public systemic::IProcess, public communication::IBinaryDataSubscriber {
public:
typedef communication::format::bits::bitsize bitsize;
BinaryEncoderSync( bitsize angle_offset, bitsize angle_size,
bitsize turn_offset, bitsize turn_size );
void read( const void * data, std::size_t size );
void process();
void setSampleTime( float ts_in_second );
//!Функция возращает номер оборота.
Turn getTurn() const;
//!Функция возращает угол в радианах от 0 до 2*pi или NaN.
Angle getAngle() const;
//!Метод позволяет получить консистентные данные позиции энкодера в формате (оборот, угол).
std::pair<Turn, Angle> getPosition() const;
private:
typedef uint32_t frame;
//todo: extract to communication
struct Bitfield {
frame mask;
bitsize offset;
frame get( frame );
Bitfield( bitsize offset, bitsize size );
} angle, turn;
float angle_cost;
peripheral::nanotimer::time_ns cycle_time;
struct Points {
struct Point {
Turn turn;
Angle angle;
peripheral::nanotimer::stamp timestamp;
} first, second;
};
std::atomic< Points > points;
std::atomic_flag handled;
DoubleBuffer< std::pair<Turn, Angle> > sample;
std::pair<Turn, Angle> per_sample( peripheral::nanotimer::stamp timestamp );
};
}}
#endif /* UMLIBRARY_DRIVER_BINARYENCODERSYNC_HH_ */