84 lines
2.0 KiB
C++
84 lines
2.0 KiB
C++
/*
|
|
* 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_ */
|