yarp-devices
Loading...
Searching...
No Matches
Jr3Mbed.hpp
1// -*- mode:C++; tab-width:4; c-basic-offset:4; indent-tabs-mode:nil -*-
2
3#ifndef __JR3_MBED_HPP__
4#define __JR3_MBED_HPP__
5
6#include <cstdint>
7
8#include <array>
9#include <atomic>
10#include <mutex>
11#include <string>
12
13#include <yarp/os/Timer.h>
14
15#include <yarp/dev/DeviceDriver.h>
16#include <yarp/dev/MultipleAnalogSensorsInterfaces.h>
17
18#include "ICanBusSharer.hpp"
19#include "StateObserver.hpp"
20#include "Jr3Mbed_ParamsParser.h"
21
32class Jr3Mbed : public yarp::dev::DeviceDriver,
33 public yarp::dev::ISixAxisForceTorqueSensors,
36{
37public:
38 // --------- DeviceDriver Declarations. Implementation in DeviceDriverImpl.cpp ---------
39
40 bool open(yarp::os::Searchable & config) override;
41 bool close() override;
42
43 // --------- ICanBusSharer declarations. Implementation in LacqueyFetch.cpp ---------
44
45 unsigned int getId() override;
46 bool notifyMessage(const roboticslab::can_message & message) override;
47 bool initialize() override;
48 bool finalize() override;
49 bool registerSender(roboticslab::ICanSenderDelegate * sender) override;
50 bool synchronize(double timestamp) override;
51
52 // --------- ISixAxisForceTorqueSensors Declarations. Implementation in ISixAxisForceTorqueSensorsImpl.cpp ---------
53
54 std::size_t getNrOfSixAxisForceTorqueSensors() const override;
55 yarp::dev::MAS_status getSixAxisForceTorqueSensorStatus(std::size_t sens_index) const override;
56 bool getSixAxisForceTorqueSensorName(std::size_t sens_index, std::string & name) const override;
57 bool getSixAxisForceTorqueSensorFrameName(std::size_t sens_index, std::string & name) const override;
58 bool getSixAxisForceTorqueSensorMeasure(std::size_t sens_index, yarp::sig::Vector & out, double & timestamp) const override;
59
60private:
61 // keep this in sync with the firmware
62 enum class can_ops : std::uint16_t
63 {
64 ACK = 0x100,
65 START_SYNC = 0x180,
66 START_ASYNC = 0x200,
67 STOP = 0x280,
68 ZERO_OFFS = 0x300,
69 SET_FILTER = 0x380,
70 GET_STATE = 0x400,
71 GET_FS_FORCES = 0x480,
72 GET_FS_MOMENTS = 0x500,
73 RESET = 0x580,
74 FORCES = 0x600,
75 MOMENTS = 0x680,
76 BOOTUP = 0x700,
77 };
78
79 // keep this in sync with the firmware
80 enum class jr3_state : std::uint8_t
81 { READY = 0x00, NOT_INITIALIZED = 0x01 };
82
83 enum class jr3_mode
84 { SYNC, ASYNC, INVALID };
85
86 constexpr unsigned int getCommandId(can_ops op) const
87 { return m_canId + static_cast<unsigned int>(op); }
88
89 bool performRequest(const std::string & cmd, const roboticslab::can_message & msg, std::uint8_t * response, bool quiet = false);
90 bool sendStartSyncCommand(double filter);
91 bool sendStartAsyncCommand(double filter, double delay);
92 bool sendCommand(const std::string & cmd, can_ops op);
93 bool ping();
94 bool queryFullScales();
95
96 bool monitorWorker(const yarp::os::YarpTimerEvent & event);
97
98 std::array<double, 6> scales;
99 bool shouldQueryFullScales {false};
100
101 jr3_mode mode {jr3_mode::INVALID};
102
103 roboticslab::ICanSenderDelegate * sender {nullptr};
104 roboticslab::TypedStateObserver<std::uint8_t[]> * ackStateObserver {nullptr};
105
106 mutable std::mutex mtx;
107
108 std::array<std::int16_t, 3> buffer {}; // zero-initialize
109 std::array<std::int16_t, 6> raw {}; // zero-initialize
110
111 std::atomic<yarp::dev::MAS_status> status {yarp::dev::MAS_UNKNOWN};
112 std::atomic_bool isBooting {false};
113 double timestamp {0.0};
114 std::uint16_t frameCounter {0};
115 double lastDiagnosticsTimestamp {0.0};
116 unsigned int lastFrameCounter {0};
117
118 yarp::os::Timer * monitorThread {nullptr};
119
120 static constexpr unsigned int FULL_SCALE = 16384; // 2^14
121};
122
123#endif // __JR3_MBED_HPP__
Definition Jr3Mbed_ParamsParser.h:49
Implementation of a CAN node on an Mbed board that publishes data from a JR3 sensor.
Definition Jr3Mbed.hpp:36
bool notifyMessage(const roboticslab::can_message &message) override
Notify observers that a new CAN message has arrived.
Definition ICanBusSharerImpl.cpp:88
unsigned int getId() override
Retrieve CAN node ID.
Definition ICanBusSharerImpl.cpp:37
bool finalize() override
Finalize CAN node communications.
Definition ICanBusSharerImpl.cpp:76
bool registerSender(roboticslab::ICanSenderDelegate *sender) override
Pass a handle to a CAN sender delegate instance.
Definition ICanBusSharerImpl.cpp:130
bool initialize() override
Perform CAN node initialization.
Definition ICanBusSharerImpl.cpp:44
bool synchronize(double timestamp) override
Perform synchronized action on CAN master's request.
Definition ICanBusSharerImpl.cpp:138
Abstract base for a CAN bus sharer.
Definition ICanBusSharer.hpp:25
Implementation-agnostic consumer for TX CAN transfers.
Definition ICanSenderDelegate.hpp:22
Type state observer for non-arithmetic types.
Definition StateObserver.hpp:95
Proxy CAN message structure.
Definition CanMessage.hpp:20