MRMS
mrm-ref-can.h
1 #pragma once
2 #include "Arduino.h"
3 #include <mrm-board.h>
4 
12 #define CAN_ID_REF_CAN0_IN 0x0160
13 #define CAN_ID_REF_CAN0_OUT 0x0161
14 #define CAN_ID_REF_CAN1_IN 0x0162
15 #define CAN_ID_REF_CAN1_OUT 0x0163
16 #define CAN_ID_REF_CAN2_IN 0x0164
17 #define CAN_ID_REF_CAN2_OUT 0x0165
18 #define CAN_ID_REF_CAN3_IN 0x0166
19 #define CAN_ID_REF_CAN3_OUT 0x0167
20 #define CAN_ID_REF_CAN4_IN 0x0168
21 #define CAN_ID_REF_CAN4_OUT 0x0169
22 #define CAN_ID_REF_CAN5_IN 0x016A
23 #define CAN_ID_REF_CAN5_OUT 0x016B
24 #define CAN_ID_REF_CAN6_IN 0x016C
25 #define CAN_ID_REF_CAN6_OUT 0x016D
26 #define CAN_ID_REF_CAN7_IN 0x016E
27 #define CAN_ID_REF_CAN7_OUT 0x016F
28 
29 #define MRM_REF_CAN_SENSOR_COUNT 9 // Number of IR transistors in each device.
30 
31 //CANBus commands
32 #define COMMAND_REF_CAN_MEASURE_ONCE_CENTER 0x04
33 #define COMMAND_REF_CAN_MEASURE_CONTINUOUS_CENTER 0x05
34 #define COMMAND_REF_CAN_SENDING_SENSORS_1_TO_3 0x06
35 #define COMMAND_REF_CAN_SENDING_SENSORS_4_TO_6 0x07
36 #define COMMAND_REF_CAN_SENDING_SENSORS_7_TO_9 0x08
37 #define COMMAND_REF_CAN_CALIBRATE 0x09
38 #define COMMAND_REF_CAN_CALIBRATION_DATA_DARK_1_TO_3 0x0A
39 #define COMMAND_REF_CAN_CALIBRATION_DATA_DARK_4_TO_6 0x0B
40 #define COMMAND_REF_CAN_CALIBRATION_DATA_DARK_7_TO_9 0x0C
41 #define COMMAND_REF_CAN_CALIBRATION_DATA_BRIGHT_1_TO_3 0x0F
42 #define COMMAND_REF_CAN_CALIBRATION_DATA_BRIGHT_4_TO_6 0x50
43 #define COMMAND_REF_CAN_CALIBRATION_DATA_BRIGHT_7_TO_9 0x51
44 #define COMMAND_REF_CAN_CALIBRATION_DATA_REQUEST 0x0D
45 #define COMMAND_REF_CAN_SENDING_SENSORS_CENTER 0x0E
46 #define COMMAND_REPORT_ALIVE_QUEUELESS 0x0F // todo
47 
48 #define MRM_REF_CAN_INACTIVITY_ALLOWED_MS 10000
49 
50 class Mrm_ref_can : public SensorBoard
51 {
52  enum mode { ANALOG_VALUES, DIGITAL_AND_BRIGHT_CENTER, DIGITAL_AND_DARK_CENTER };
53 
54  std::vector<uint16_t[MRM_REF_CAN_SENSOR_COUNT]>* calibrationDataDark; //
55  std::vector<uint16_t[MRM_REF_CAN_SENSOR_COUNT]>* calibrationDataBright;
56  std::vector<uint8_t>* dataFresh; // All the data refreshed, bitwise stored.
57  // Most significant bit 0: readings for transistors 1 - 3,
58  // bit 1: 4 - 6,
59  // bit 2: 7 - 9,
60  // bit 3: calibration data for transistors 1 - 3,
61  // bit 4: 4 - 6,
62  // bit 5: 7 - 9
63  std::vector<uint8_t>* _mode;
64  bool readingDigitalAndCenter = true; // Reading only center and transistors as bits. Otherwise reading all transistors as analog values.
65  std::vector<uint16_t[MRM_REF_CAN_SENSOR_COUNT]>* _reading; // Analog or digital readings of all sensors, depending on measuring mode.
66  std::vector<uint16_t>* centerOfMeasurements; // Center of the dark sensors.
67 
72  bool analogStarted(uint8_t deviceNumber);
73 
78  bool dataCalibrationFreshAsk(uint8_t deviceNumber) { return ((*dataFresh)[deviceNumber] & 0b00011100) == 0b00011100; }
79 
84  bool dataFreshAsk(uint8_t deviceNumber) { return (*dataFresh)[deviceNumber] == 0xFF; }
85 
90  void dataFreshCalibrationSet(bool setToFresh, uint8_t deviceNumber = 0);
91 
96  void dataFreshReadingsSet(bool setToFresh, uint8_t deviceNumber = 0);
97 
104  bool digitalStarted(uint8_t deviceNumber, bool darkCenter, bool startIfNot = true);
105 
106 public:
107 
114  Mrm_ref_can(Robot* robot = NULL, uint8_t maxNumberOfBoards = 4);
115 
116  ~Mrm_ref_can();
117 
121  void add(char * deviceName = (char*)"");
122 
129  bool any(bool dark = true, uint8_t deviceNumber = 0, uint8_t fistTransistor = 0, uint8_t lastTransistor = 0xFF);
130 
134  void calibrate(uint8_t deviceNumber = 0xFF);
135 
142  uint16_t calibrationDataGet(uint8_t receiverNumberInSensor, bool isDark, uint8_t deviceNumber = 0);
143 
146  void calibrationPrint();
147 
152  void calibrationDataRequest(uint8_t deviceNumber = 0, bool waitForResult = false);
153 
159  uint16_t center(uint8_t deviceNumber = 0, bool ofDark = true);
160 
167  bool dark(uint8_t receiverNumberInSensor, uint8_t deviceNumber = 0, bool fromAnalog = false);
168 
173  bool messageDecode(uint32_t canId, uint8_t data[8]);
174 
180  uint16_t reading(uint8_t receiverNumberInSensor, uint8_t deviceNumber = 0);
181 
184  void readingsPrint();
185 
189  void test(bool analog);
190 
191 };
192 
193 
194 
Definition: mrm-ref-can.h:51
void calibrationPrint()
Definition: mrm-ref-can.cpp:197
bool dark(uint8_t receiverNumberInSensor, uint8_t deviceNumber=0, bool fromAnalog=false)
Definition: mrm-ref-can.cpp:229
Mrm_ref_can(Robot *robot=NULL, uint8_t maxNumberOfBoards=4)
Definition: mrm-ref-can.cpp:10
void readingsPrint()
Definition: mrm-ref-can.cpp:429
uint16_t center(uint8_t deviceNumber=0, bool ofDark=true)
Definition: mrm-ref-can.cpp:216
uint16_t reading(uint8_t receiverNumberInSensor, uint8_t deviceNumber=0)
Definition: mrm-ref-can.cpp:415
void test(bool analog)
Definition: mrm-ref-can.cpp:441
void calibrate(uint8_t deviceNumber=0xFF)
Definition: mrm-ref-can.cpp:125
bool messageDecode(uint32_t canId, uint8_t data[8])
Definition: mrm-ref-can.cpp:316
void calibrationDataRequest(uint8_t deviceNumber=0, bool waitForResult=false)
Definition: mrm-ref-can.cpp:173
void add(char *deviceName=(char *)"")
Definition: mrm-ref-can.cpp:27
uint16_t calibrationDataGet(uint8_t receiverNumberInSensor, bool isDark, uint8_t deviceNumber=0)
Definition: mrm-ref-can.cpp:160
bool any(bool dark=true, uint8_t deviceNumber=0, uint8_t fistTransistor=0, uint8_t lastTransistor=0xFF)
Definition: mrm-ref-can.cpp:105
Definition: mrm-robot.h:42
Definition: mrm-board.h:356