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/conf/version.h>
14
15#include <yarp/os/Timer.h>
16
17#include <yarp/dev/DeviceDriver.h>
18#include <yarp/dev/MultipleAnalogSensorsInterfaces.h>
19
20#include "ICanBusSharer.hpp"
21#include "StateObserver.hpp"
22#include "Jr3Mbed_ParamsParser.h"
23
34class Jr3Mbed : public yarp::dev::DeviceDriver,
35 public yarp::dev::ISixAxisForceTorqueSensors,
38{
39public:
40 // --------- DeviceDriver Declarations. Implementation in DeviceDriverImpl.cpp ---------
41
42 bool open(yarp::os::Searchable & config) override;
43 bool close() override;
44
45 // --------- ICanBusSharer declarations. Implementation in LacqueyFetch.cpp ---------
46
47 unsigned int getId() override;
48 bool notifyMessage(const roboticslab::can_message & message) override;
49 bool initialize() override;
50 bool finalize() override;
51 bool registerSender(roboticslab::ICanSenderDelegate * sender) override;
52 bool synchronize(double timestamp) override;
53
54 // --------- ISixAxisForceTorqueSensors Declarations. Implementation in ISixAxisForceTorqueSensorsImpl.cpp ---------
55
56#if YARP_VERSION_COMPARE(>=, 4, 1, 0) || defined(YARP_NEXT)
57 yarp::dev::ReturnValue getNrOfSixAxisForceTorqueSensors(std::size_t & num) const override;
58 yarp::dev::MAS_status getSixAxisForceTorqueSensorStatus(std::size_t sens_index) const override;
59 yarp::dev::ReturnValue getSixAxisForceTorqueSensorName(std::size_t sens_index, std::string & name) const override;
60 yarp::dev::ReturnValue getSixAxisForceTorqueSensorFrameName(std::size_t sens_index, std::string & frameName) const override;
61 yarp::dev::ReturnValue getSixAxisForceTorqueSensorMeasure(std::size_t sens_index, yarp::sig::Vector & out, double & timestamp) const override;
62#else
63 std::size_t getNrOfSixAxisForceTorqueSensors() const override;
64 yarp::dev::MAS_status getSixAxisForceTorqueSensorStatus(std::size_t sens_index) const override;
65 bool getSixAxisForceTorqueSensorName(std::size_t sens_index, std::string & name) const override;
66 bool getSixAxisForceTorqueSensorFrameName(std::size_t sens_index, std::string & frameName) const override;
67 bool getSixAxisForceTorqueSensorMeasure(std::size_t sens_index, yarp::sig::Vector & out, double & timestamp) const override;
68#endif
69
70private:
71 // keep this in sync with the firmware
72 enum class can_ops : std::uint16_t
73 {
74 ACK = 0x100,
75 START_SYNC = 0x180,
76 START_ASYNC = 0x200,
77 STOP = 0x280,
78 ZERO_OFFS = 0x300,
79 SET_FILTER = 0x380,
80 GET_STATE = 0x400,
81 GET_FS_FORCES = 0x480,
82 GET_FS_MOMENTS = 0x500,
83 RESET = 0x580,
84 FORCES = 0x600,
85 MOMENTS = 0x680,
86 BOOTUP = 0x700,
87 };
88
89 // keep this in sync with the firmware
90 enum class jr3_state : std::uint8_t
91 { READY = 0x00, NOT_INITIALIZED = 0x01 };
92
93 enum class jr3_mode
94 { SYNC, ASYNC, INVALID };
95
96 constexpr unsigned int getCommandId(can_ops op) const
97 { return m_canId + static_cast<unsigned int>(op); }
98
99 bool performRequest(const std::string & cmd, const roboticslab::can_message & msg, std::uint8_t * response, bool quiet = false);
100 bool sendStartSyncCommand(double filter);
101 bool sendStartAsyncCommand(double filter, double delay);
102 bool sendCommand(const std::string & cmd, can_ops op);
103 bool ping();
104 bool queryFullScales();
105
106 bool monitorWorker(const yarp::os::YarpTimerEvent & event);
107
108 std::array<double, 6> scales;
109 bool shouldQueryFullScales {false};
110
111 jr3_mode mode {jr3_mode::INVALID};
112
113 roboticslab::ICanSenderDelegate * sender {nullptr};
114 roboticslab::TypedStateObserver<std::uint8_t[]> * ackStateObserver {nullptr};
115
116 mutable std::mutex mtx;
117
118 std::array<std::int16_t, 3> buffer {}; // zero-initialize
119 std::array<std::int16_t, 6> raw {}; // zero-initialize
120
121 std::atomic<yarp::dev::MAS_status> status {yarp::dev::MAS_UNKNOWN};
122 std::atomic_bool isBooting {false};
123 double timestamp {0.0};
124 std::uint16_t frameCounter {0};
125 double lastDiagnosticsTimestamp {0.0};
126 unsigned int lastFrameCounter {0};
127
128 yarp::os::Timer * monitorThread {nullptr};
129
130 static constexpr unsigned int FULL_SCALE = 16384; // 2^14
131};
132
133#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:38
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