Thunderbots Project
Loading...
Searching...
No Matches
threaded_estop_reader.h
1#pragma once
2
3#pragma once
4
5
6#include <boost/asio.hpp>
7#include <iostream>
8#include <mutex>
9#include <thread>
10
11#include "shared/constants.h"
12#include "software/uart/uart_communication.h"
13#include "software/util/make_enum/make_enum.hpp"
14
15// enum that represents the possible states of estop
16MAKE_ENUM(EstopState, PLAY, STOP, STATUS_ERROR);
17
18/*
19 * class that reads status of estop at periodic intervals of time
20 */
22{
23 public:
29 ThreadedEstopReader(std::unique_ptr<UartCommunication> uart_reader);
31
35 bool isEstopPlay();
36
37
38 private:
39 /*
40 * handler method that is called every time the timer expires and a new read is
41 * requested
42 */
43 void tick(const boost::system::error_code&);
44
45 /*
46 * method that initiates timer
47 */
48 void continousRead();
49
50 // In the case where we read an unknown message (not PLAY or STOP) we try again this
51 // number of times
52 static constexpr unsigned int MAXIMUM_CONSECUTIVE_STATUS_ERROR = 5;
53
54 // time between consecutive reads
55 static constexpr int INTERVAL_BETWEEN_READS_MS = 5;
56
57 // thread that will periodically pull values from the buffer
58 std::thread estop_thread;
59 std::atomic_bool in_destructor = false;
60
61 // current state of estop
62 std::atomic<EstopState> estop_state;
63
64 // tracks the number of unknown messages received in a row
65 unsigned int num_consecutive_status_error = 0;
66
67 // Time between reads
68 unsigned int regular_read_interval_ms;
69
70
71 // boost construct for managing io operations
72 boost::asio::io_service io_service;
73
74 // timer that expires after every specified interval of time
75 boost::asio::deadline_timer timer;
76
77 // the UART device acting as source of estop values
78 std::unique_ptr<UartCommunication> uart_reader;
79};
Definition threaded_estop_reader.h:22
bool isEstopPlay()
Definition threaded_estop_reader.cpp:25