YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
broadcastingThread.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2024 Istituto Italiano di Tecnologia (IIT)
3 * SPDX-License-Identifier: BSD-3-Clause
4 */
5
6#include <yarp/os/Time.h>
7#include <yarp/os/Log.h>
8#include <yarp/os/LogStream.h>
11
12#include "robotDriver.h"
13#include "robotAction.h"
14#include "broadcastingThread.h"
15
16
17BroadcastingThread::BroadcastingThread(std::string name, robotDriver *p, action_class *a, double period): PeriodicThread(period)
18{
19 yAssert(p != nullptr);
20 yAssert(a != nullptr);
21
22 module_name = name;
23 driver = p;
24 actions = a;
25}
26
28{
29 port_data_out.interrupt();
30 port_data_out.close();
31}
32
34{
35 if (!port_data_out.open(std::string("/") + module_name + "/all_joints_data_out:o"));
36 {
37 return false;
38 }
39
40 if (!driver)
41 {
42 return false;
43 }
44
45 if (!actions)
46 {
47 return false;
48 }
49
50 njoints = driver->getNJoints();
51 encs.resize(njoints);
52 outs.resize(njoints);
53 errs.resize(njoints);
54 mots.resize(njoints);
55
56 return true;
57}
58
60{
61 //reads the current position
62 if (driver && driver->ienc_ll)
63 {
64 driver->ienc_ll->getEncoders(encs.data());
65 }
66 else
67 {
68 //invalid driver
69 }
70
71 //reads the pid output
72 if (driver && driver->ipid_ll)
73 {
75 }
76 else
77 {
78 //invalid driver
79 }
80
81 //reads the pid error
82 if (driver && driver->ipid_ll)
83 {
85 }
86 else
87 {
88 //invalid driver
89 }
90
91 //reads the motor encoders
92 if (driver && driver->imotenc_ll)
93 {
94 driver->imotenc_ll->getMotorEncoders(mots.data());
95 }
96 else
97 {
98 //invalid driver
99 }
100
101 size_t j = actions->current_frame;
102
103 yarp::os::Bottle& bot2 = this->port_data_out.prepare();
104 bot2.clear();
105 bot2.addInt32((int)actions->action_frames_vector[j].counter);
106 bot2.addFloat64(actions->action_frames_vector[j].time);
107
108 size_t size = this->actions->action_frames_vector[j].q_joints.size();
109 double *ll = actions->action_frames_vector[j].q_joints.data();
110
111 bot2.addString("commands:");
112 for (int ix=0;ix<size;ix++)
113 {
114 bot2.addFloat64(ll[ix]);
115 }
116 bot2.addString("joint encoders:");
117 for (int ix=0;ix<size;ix++)
118 {
119 bot2.addFloat64(encs[ix]);
120 }
121 bot2.addString("outputs:");
122 for (int ix=0;ix<size;ix++)
123 {
124 bot2.addFloat64(outs[ix]);
125 }
126 bot2.addString("motor encoders:");
127 for (int ix=0;ix<size;ix++)
128 {
129 bot2.addFloat64(mots[ix]);
130 }
131 bot2.addString("errors:");
132 for (int ix=0;ix<size;ix++)
133 {
134 bot2.addFloat64(errs[ix]);
135 }
136 bot2.addString("timestamp:");
138 this->port_data_out.write();
139}
#define yAssert(x)
Definition Log.h:388
bool threadInit() override
Initialization method.
void run() override
Loop function.
BroadcastingThread(std::string module_name, robotDriver *p, action_class *a, double period=0.001)
size_t current_frame
Definition robotAction.h:50
std::deque< action_frame > action_frames_vector
Definition robotAction.h:53
size_t getNJoints()
virtual bool getEncoders(double *encs)=0
Read the position of all axes.
virtual bool getMotorEncoders(double *encs)=0
Read the position of all motor encoders.
virtual bool getPidOutputs(const PidControlTypeEnum &pidtype, double *outs)=0
Get the output of the controllers (e.g.
virtual bool getPidErrors(const PidControlTypeEnum &pidtype, double *errs)=0
Get the error of all joints.
A simple collection of objects that can be described and transmitted in a portable way.
Definition Bottle.h:64
void addFloat64(yarp::conf::float64_t x)
Places a 64-bit floating point number in the bottle, at the end of the list.
Definition Bottle.cpp:158
void clear()
Empties the bottle of any objects it contains.
Definition Bottle.cpp:121
void addInt32(std::int32_t x)
Places a 32-bit integer in the bottle, at the end of the list.
Definition Bottle.cpp:140
void addString(const char *str)
Places a string in the bottle, at the end of the list.
Definition Bottle.cpp:170
void close() override
Stop port activity.
bool open(const std::string &name) override
Start port operation, with a specific name, with automatically-chosen network parameters.
void interrupt() override
Interrupt any current reads or writes attached to the port.
void write(bool forceStrict=false)
Write the current object being returned by BufferedPort::prepare.
T & prepare()
Access the object which will be transmitted by the next call to yarp::os::BufferedPort::write.
@ VOCAB_PIDTYPE_POSITION
Definition PidEnums.h:16
double now()
Return the current time in seconds, relative to an arbitrary starting point.
Definition Time.cpp:121