Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
Open Dynamic Robot Initiative
Web Operator
Commits
cd2cd2e5
Commit
cd2cd2e5
authored
Oct 15, 2021
by
Guilhem Saurel
Browse files
commands
parent
7e4b9007
Changes
7
Hide whitespace changes
Inline
Side-by-side
CMakeLists.txt
View file @
cd2cd2e5
...
...
@@ -2,13 +2,12 @@ cmake_minimum_required (VERSION 3.5)
project
(
mqtt-interface
)
find_package
(
Eigen3 REQUIRED
)
find_package
(
Threads REQUIRED
)
add_subdirectory
(
paho.mqtt.c
)
add_library
(
mqtt-interface SHARED include/qrw/mqtt-interface.hpp src/mqtt-interface.cpp
)
target_link_libraries
(
mqtt-interface PRIVATE paho-mqtt3c paho-mqtt3a
Eigen3::Eigen
)
target_link_libraries
(
mqtt-interface PRIVATE paho-mqtt3c paho-mqtt3a
)
target_include_directories
(
mqtt-interface PUBLIC $<BUILD_INTERFACE:
${
CMAKE_SOURCE_DIR
}
/include>
)
add_executable
(
mock src/main.cpp
)
target_link_libraries
(
mock PUBLIC mqtt-interface
Eigen3::Eigen
Threads::Threads
)
target_link_libraries
(
mock PUBLIC mqtt-interface Threads::Threads
)
README.md
View file @
cd2cd2e5
...
...
@@ -26,3 +26,20 @@ yarn install
yarn parce src/index.html
./build/mock
```
## Topics
### Telemetry
```
/odri/voltage
/odri/current
/odri/joystick
```
### Commands
```
/odri/commands calibrate
/odri/commands stop
```
include/qrw/mqtt-interface.hpp
View file @
cd2cd2e5
#ifndef MQTT_INTERFACE_HPP
#define MQTT_INTERFACE_HPP
#include
<Eigen/Dense>
#include
<mutex>
typedef
Eigen
::
Matrix
<
double
,
4
,
1
>
MyVector4d
;
class
MqttInterface
{
public:
MqttInterface
();
void
set
(
const
MyVector4d
&
values
,
unsigned
char
battery
);
void
set
(
unsigned
char
current
,
unsigned
char
voltage
,
std
::
string
&
joystick
);
void
start
();
void
stop
();
bool
getStop
();
bool
getCalibrate
();
private:
bool
run_
;
MyVector4d
values_
;
unsigned
char
battery_
;
unsigned
char
current_
;
unsigned
char
voltage_
;
std
::
string
joystick_
;
std
::
mutex
run_m
;
std
::
mutex
data_m
;
bool
getRun
();
std
::
string
getValues
();
std
::
string
getBattery
();
std
::
string
getCurrent
();
std
::
string
getVoltage
();
std
::
string
getJoystick
();
};
#endif
/* !MQTT_INTERFACE_HPP */
src/app.js
View file @
cd2cd2e5
...
...
@@ -6,24 +6,27 @@ const app = Vue.createApp({
data
()
{
return
{
client
:
'
-
'
,
battery
:
'
-
'
,
values
:
'
-
'
voltage
:
'
-
'
,
current
:
'
-
'
,
joystick
:
'
-
'
}
},
methods
:
{
onCommand
()
{
console
.
log
(
'
ok
'
);
this
.
client
.
publish
(
'
/odri/commands
'
,
'
command
'
);
onCommand
(
message
)
{
this
.
client
.
publish
(
'
/odri/commands
'
,
message
);
},
onConnect
()
{
this
.
client
.
subscribe
(
'
/odri/battery
'
);
this
.
client
.
subscribe
(
'
/odri/values
'
);
this
.
client
.
subscribe
([
'
/odri/voltage
'
,
'
/odri/current
'
,
'
/odri/joystick
'
]);
},
onMessage
(
topic
,
message
)
{
if
(
topic
==
'
/odri/battery
'
)
{
this
.
battery
=
message
.
toString
();
if
(
topic
==
'
/odri/voltage
'
)
{
this
.
voltage
=
message
.
toString
();
}
else
if
(
topic
==
'
/odri/current
'
)
{
this
.
current
=
message
.
toString
();
}
else
if
(
topic
==
'
/odri/joystick
'
)
{
this
.
joystick
=
message
.
toString
();
}
else
{
this
.
values
=
message
.
toString
(
);
console
.
log
(
"
unknow topic
"
,
topic
);
}
}
},
...
...
src/index.html
View file @
cd2cd2e5
...
...
@@ -10,9 +10,13 @@
<h1>
Hello, Solo!
</h1>
<main>
<div
id=
"app"
>
<p>
battery: {{ battery }}%
</p>
<p>
values: {{ values }}
</p>
<button
type=
"button"
class=
"btn btn-primary"
v-on:click=
"onCommand"
>
Send Command
</button>
<dl
class=
"row"
>
<dt
class=
"col-sm-3"
>
Voltage
</dt><dd
class=
"col-sm-9"
>
{{ voltage }}
</dd>
<dt
class=
"col-sm-3"
>
Current
</dt><dd
class=
"col-sm-9"
>
{{ current }}
</dd>
<dt
class=
"col-sm-3"
>
Joystick
</dt><dd
class=
"col-sm-9"
>
{{ joystick }}
</dd>
</dl>
<button
type=
"button"
class=
"btn btn-warning"
v-on:click=
"onCommand('calibrate')"
>
Calibrate
</button>
<button
type=
"button"
class=
"btn btn-danger"
v-on:click=
"onCommand('stop')"
>
Stop
</button>
</div>
</main>
</div>
...
...
src/main.cpp
View file @
cd2cd2e5
...
...
@@ -4,14 +4,21 @@
#include
"qrw/mqtt-interface.hpp"
int
main
()
{
MyVector4d
values
;
unsigned
char
current
=
12
;
std
::
string
joystick
=
"go"
;
MqttInterface
mqtt_interface
;
std
::
thread
mqtt_thread
(
&
MqttInterface
::
start
,
&
mqtt_interface
);
for
(
unsigned
char
battery
=
100
;
battery
>
72
;
battery
--
)
{
values
.
setRandom
();
mqtt_interface
.
set
(
values
,
battery
);
for
(
unsigned
char
voltage
=
100
;
voltage
>
72
;
voltage
--
)
{
mqtt_interface
.
set
(
current
,
voltage
,
joystick
);
if
(
mqtt_interface
.
getStop
())
{
std
::
cout
<<
"STOP !"
<<
std
::
endl
;
break
;
}
if
(
mqtt_interface
.
getCalibrate
())
{
std
::
cout
<<
"let's calibrate"
<<
std
::
endl
;
}
std
::
this_thread
::
sleep_for
(
std
::
chrono
::
milliseconds
(
250
));
}
...
...
src/mqtt-interface.cpp
View file @
cd2cd2e5
...
...
@@ -2,17 +2,30 @@
#include
<iostream>
#include
<thread>
#include
<cstring>
#include
"MQTTClient.h"
#define ADDRESS "tcp://localhost:1883"
#define CLIENTID "Gepetto QRW MQTT Interface"
bool
stop_
=
false
;
bool
calibrate_
=
false
;
std
::
mutex
commands_m
;
void
delivered
(
void
*
/*context*/
,
MQTTClient_deliveryToken
/*dt*/
)
{}
int
msgarrvd
(
void
*
context
,
char
*
topicName
,
int
topicLen
,
MQTTClient_message
*
message
)
{
std
::
string
payload
((
char
*
)
message
->
payload
,
message
->
payloadlen
);
std
::
cout
<<
"Message arrived on "
<<
topicName
<<
": "
<<
payload
<<
std
::
endl
;
if
(
payload
==
"stop"
)
{
std
::
lock_guard
<
std
::
mutex
>
guard
(
commands_m
);
stop_
=
true
;
}
else
if
(
payload
==
"calibrate"
)
{
std
::
lock_guard
<
std
::
mutex
>
guard
(
commands_m
);
calibrate_
=
true
;
}
else
{
std
::
cerr
<<
"Unknown Message on "
<<
topicName
<<
": "
<<
payload
<<
std
::
endl
;
}
MQTTClient_freeMessage
(
&
message
);
MQTTClient_free
(
topicName
);
...
...
@@ -21,18 +34,17 @@ int msgarrvd(void *context, char *topicName, int topicLen, MQTTClient_message *m
void
connlost
(
void
*
context
,
char
*
cause
)
{
std
::
cerr
<<
"Connection lost"
<<
std
::
endl
;
;
std
::
cerr
<<
"cause: "
<<
cause
<<
std
::
endl
;
;
}
void
MqttInterface
::
set
(
const
MyVector4d
&
values
,
unsigned
char
battery
)
{
void
MqttInterface
::
set
(
unsigned
char
current
,
unsigned
char
voltage
,
std
::
string
&
joystick
)
{
std
::
lock_guard
<
std
::
mutex
>
guard
(
data_m
);
values_
=
values
;
battery_
=
battery
;
current_
=
current
;
voltage_
=
voltage
;
joystick_
=
joystick
;
}
MqttInterface
::
MqttInterface
()
:
run_
(
true
),
values_
(
MyVector4d
::
Zero
()),
battery
_
(
0
)
{}
MqttInterface
::
MqttInterface
()
:
run_
(
true
),
current_
(
0
),
voltage_
(
0
),
joystick
_
()
{}
void
MqttInterface
::
start
()
{
int
rc
;
...
...
@@ -50,19 +62,24 @@ void MqttInterface::start() {
MQTTClient_subscribe
(
client
,
"/odri/commands"
,
0
);
MQTTClient_deliveryToken
token
;
std
::
string
values_s
;
std
::
string
battery_s
;
char
*
values_c
;
char
*
battery_c
;
std
::
string
current_s
;
std
::
string
voltage_s
;
std
::
string
joystick_s
;
const
char
*
current_c
;
const
char
*
voltage_c
;
const
char
*
joystick_c
;
while
(
getRun
())
{
values_s
=
getValues
();
battery_s
=
getBattery
();
values_c
=
values_s
.
data
();
battery_c
=
battery_s
.
data
();
current_s
=
getCurrent
();
voltage_s
=
getVoltage
();
joystick_s
=
getJoystick
();
current_c
=
current_s
.
data
();
voltage_c
=
voltage_s
.
data
();
joystick_c
=
joystick_s
.
data
();
MQTTClient_publish
(
client
,
"/odri/battery"
,
strlen
(
battery_c
),
battery_c
,
0
,
0
,
&
token
);
MQTTClient_publish
(
client
,
"/odri/values"
,
strlen
(
values_c
),
values_c
,
0
,
0
,
&
token
);
MQTTClient_publish
(
client
,
"/odri/current"
,
std
::
strlen
(
current_c
),
current_c
,
0
,
0
,
&
token
);
MQTTClient_publish
(
client
,
"/odri/voltage"
,
std
::
strlen
(
voltage_c
),
voltage_c
,
0
,
0
,
&
token
);
MQTTClient_publish
(
client
,
"/odri/joystick"
,
std
::
strlen
(
joystick_c
),
joystick_c
,
0
,
0
,
&
token
);
std
::
this_thread
::
sleep_for
(
std
::
chrono
::
milliseconds
(
100
));
}
...
...
@@ -76,20 +93,42 @@ void MqttInterface::stop() {
run_
=
false
;
}
bool
MqttInterface
::
getStop
()
{
std
::
lock_guard
<
std
::
mutex
>
guard
(
commands_m
);
if
(
stop_
)
{
stop_
=
false
;
return
true
;
}
return
false
;
}
bool
MqttInterface
::
getCalibrate
()
{
std
::
lock_guard
<
std
::
mutex
>
guard
(
commands_m
);
if
(
calibrate_
)
{
calibrate_
=
false
;
return
true
;
}
return
false
;
}
bool
MqttInterface
::
getRun
()
{
std
::
lock_guard
<
std
::
mutex
>
guard
(
run_m
);
return
run_
;
}
std
::
string
MqttInterface
::
getValues
()
{
std
::
string
MqttInterface
::
getJoystick
()
{
std
::
lock_guard
<
std
::
mutex
>
guard
(
data_m
);
return
joystick_
;
}
std
::
string
MqttInterface
::
getCurrent
()
{
std
::
lock_guard
<
std
::
mutex
>
guard
(
data_m
);
std
::
stringstream
ret
;
ret
<<
values_
.
transpose
();
return
ret
.
str
();
std
::
string
ret
=
std
::
to_string
(
current_
);
return
ret
;
}
std
::
string
MqttInterface
::
get
Battery
()
{
std
::
string
MqttInterface
::
get
Voltage
()
{
std
::
lock_guard
<
std
::
mutex
>
guard
(
data_m
);
std
::
string
ret
=
std
::
to_string
(
battery
_
);
std
::
string
ret
=
std
::
to_string
(
voltage
_
);
return
ret
;
}
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment