Open FFBoard
Open source force feedback firmware
EncoderBissC.h
Go to the documentation of this file.
1/*
2 * EncoderBissC.h
3 *
4 * Created on: 19.12.2020
5 * Author: Leon & Yannick
6 */
7
8#ifndef ENCODERBISSC_H_
9#define ENCODERBISSC_H_
10
11#include "cppmain.h"
12#include "constants.h"
13#include "ChoosableClass.h"
14#include "CommandHandler.h"
15#include "SPI.h"
16#include "Encoder.h"
17#include "cpp_target_config.h"
18#include <math.h>
19#include "PersistentStorage.h"
20#include "semaphore.hpp"
21#include "array"
22
24public:
25
28
30 virtual ~EncoderBissC();
31
33 static bool isCreatable();
35 int32_t getPos();
36 int32_t getPosAbs();
37 void setPos(int32_t pos);
38 uint32_t getCpr();
39
40 void saveFlash(); // Write to flash here
41 void restoreFlash(); // Load from flash
42
43 void Run();
44
46
47 CommandStatus command(const ParsedCommand& cmd,std::vector<CommandReply>& replies);
48
49 void registerCommands();
50 void configSPI();
52 void spiRxCompleted(SPIPort* port) override;
53 void beginSpiTransfer(SPIPort* port);
54 void endSpiTransfer(SPIPort* port);
55
56private:
57 static const bool useWaitSem = true; // Wait until data is processed. otherwise returns last value
58 static const uint32_t waitThresh = 2; // If last sample older than x ms use wait semaphore. Else skip and use last value to speed up processing
59 int lenghtDataBit = 22;
60 int spiSpeed = 3;
61 bool waitData = false;
62 bool invertDirection = true; // Most biss-c encoders count UP clockwise while standard motor direction is usually CCW
63
64 uint32_t lastUpdateTick = 0;
65
66 int32_t pos = 0, posOffset = 0,lastPos = 0,newPos = 0;
67 int32_t mtpos = 0;
68 //bool crc_ok = false;
69 const static uint8_t bytes = 8; // Maybe use higher length to allow higher speeds because the fixed length start timeout wastes more bits at higher rates
70 uint8_t spi_buf[bytes] = {0};
71 uint32_t decod_buf[bytes/4] = {0};
72
73
74 const uint8_t POLY = 0x43;
75 static std::array<uint8_t,64> tableCRC6n;
76 int32_t numErrors = 0;
77 static bool inUse;
80};
81
82#endif /* ENCODERBISSC_H_ */
CommandStatus
EncoderType
Definition: Encoder.h:27
int32_t lastPos
Definition: EncoderBissC.h:66
static ClassIdentifier info
Definition: EncoderBissC.h:26
void endSpiTransfer(SPIPort *port)
int32_t numErrors
Definition: EncoderBissC.h:76
const uint8_t POLY
Definition: EncoderBissC.h:74
void registerCommands()
static std::array< uint8_t, 64 > tableCRC6n
Definition: EncoderBissC.h:75
int32_t getPosAbs()
bool updateFrame()
void beginSpiTransfer(SPIPort *port)
uint32_t getCpr()
CommandStatus command(const ParsedCommand &cmd, std::vector< CommandReply > &replies)
cpp_freertos::BinarySemaphore waitForUpdateSem
Definition: EncoderBissC.h:79
void spiRxCompleted(SPIPort *port) override
static bool isCreatable()
static const uint32_t waitThresh
Definition: EncoderBissC.h:58
uint32_t lastUpdateTick
Definition: EncoderBissC.h:64
void acquirePosition()
void restoreFlash()
uint8_t spi_buf[bytes]
Definition: EncoderBissC.h:70
virtual ~EncoderBissC()
void configSPI()
static const bool useWaitSem
Definition: EncoderBissC.h:57
const ClassIdentifier getInfo()
Command handlers always have class infos. Works well with ChoosableClass.
int32_t pos
Definition: EncoderBissC.h:66
int32_t posOffset
Definition: EncoderBissC.h:66
bool invertDirection
Definition: EncoderBissC.h:62
void setPos(int32_t pos)
static bool inUse
Definition: EncoderBissC.h:77
static const uint8_t bytes
Definition: EncoderBissC.h:69
int32_t newPos
Definition: EncoderBissC.h:66
int32_t mtpos
Definition: EncoderBissC.h:67
void saveFlash()
int32_t getPos()
cpp_freertos::BinarySemaphore requestNewDataSem
Definition: EncoderBissC.h:78
uint32_t decod_buf[bytes/4]
Definition: EncoderBissC.h:71
EncoderType getEncoderType()
Definition: SPI.h:100
Definition: SPI.h:43