-
Notifications
You must be signed in to change notification settings - Fork 7
/
ICanBusSharerImpl.cpp
143 lines (113 loc) · 3.67 KB
/
ICanBusSharerImpl.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// -*- mode:C++; tab-width:4; c-basic-offset:4; indent-tabs-mode:nil -*-
#include "Jr3Mbed.hpp"
#include <cstdint>
#include <cstring>
#include <algorithm> // std::copy
#include <tuple>
#include <yarp/os/LogStream.h>
#include <yarp/os/SystemClock.h>
#include "LogComponent.hpp"
using namespace roboticslab;
// -----------------------------------------------------------------------------
namespace
{
std::tuple<std::array<std::int16_t, 3>, std::uint16_t> parseData(const can_message & message)
{
std::uint64_t data;
std::memcpy(&data, message.data, message.len);
return {{
static_cast<std::int16_t> (data & 0x000000000000FFFF),
static_cast<std::int16_t>((data & 0x00000000FFFF0000) >> 16),
static_cast<std::int16_t>((data & 0x0000FFFF00000000) >> 32)
}, static_cast<std::uint16_t>((data & 0xFFFF000000000000) >> 48)};
}
}
// -----------------------------------------------------------------------------
unsigned int Jr3Mbed::getId()
{
return canId;
}
// -----------------------------------------------------------------------------
bool Jr3Mbed::initialize()
{
if (!ping())
{
return false;
}
bool ret = false;
switch (mode)
{
case jr3_mode::SYNC:
ret = sendStartSyncCommand(filter);
break;
case jr3_mode::ASYNC:
ret = sendStartAsyncCommand(filter, asyncPeriod);
break;
default:
yCIError(JR3M, id()) << "Unknown mode:" << static_cast<int>(mode);
return false;
}
ret = ret && (!shouldQueryFullScales || queryFullScales());
// wait for the sensor data to settle, otherwise zeroing will be inaccurate
ret = ret && (yarp::os::SystemClock::delaySystem(0.1), sendCommand("zero offsets", can_ops::ZERO_OFFS));
return ret;
}
// -----------------------------------------------------------------------------
bool Jr3Mbed::finalize()
{
if (monitorThread && monitorThread->isRunning())
{
monitorThread->stop();
}
return sendCommand("stop", can_ops::STOP);
}
// -----------------------------------------------------------------------------
bool Jr3Mbed::notifyMessage(const can_message & message)
{
switch (static_cast<can_ops>(message.id - canId))
{
case can_ops::BOOTUP:
{
yCIInfo(JR3M, id()) << "Bootup message received";
isBooting = true;
// can't block here, let the monitor thread call the initialization routine
return true;
}
case can_ops::ACK:
return ackStateObserver->notify(message.data, message.len);
case can_ops::FORCES:
{
auto [forces, counter] = parseData(message);
std::lock_guard lock(mtx);
buffer = forces;
frameCounter = counter;
return true;
}
case can_ops::MOMENTS:
{
auto [moments, counter] = parseData(message);
if (std::lock_guard lock(mtx); counter == frameCounter)
{
std::copy(buffer.cbegin(), buffer.cend(), raw.begin());
std::copy(moments.cbegin(), moments.cend(), raw.begin() + 3);
timestamp = yarp::os::SystemClock::nowSystem();
}
return true;
}
default:
yCIWarning(JR3M, id(), "Unsupported operation: 0x%02x", message.id - canId);
return false;
}
}
// -----------------------------------------------------------------------------
bool Jr3Mbed::registerSender(ICanSenderDelegate * sender)
{
this->sender = sender;
return true;
}
// -----------------------------------------------------------------------------
bool Jr3Mbed::synchronize(double timestamp)
{
return true;
}
// -----------------------------------------------------------------------------