Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
test-serial-mbot.cpp
1#include <visp3/core/vpColVector.h>
2#include <visp3/core/vpConfig.h>
3#include <visp3/core/vpSerial.h>
4#include <visp3/core/vpTime.h>
5
6int main(int argc, char *argv[])
7{
8#if !defined(_WIN32)
9#ifdef ENABLE_VISP_NAMESPACE
10 using namespace VISP_NAMESPACE_NAME;
11#endif
12
13 double time = 4;
14 double v_x = 0;
15 double w_z = 0;
16 bool rpm_command = false;
17 int rpm_l = 0;
18 int rpm_r = 0;
19
20 for (int i = 1; i < argc; i++) {
21 if ((std::string(argv[i]) == "--t" || std::string(argv[i]) == "-t") && i + 1 < argc) {
22 time = static_cast<double>(atof(argv[++i]));
23 }
24 else if ((std::string(argv[i]) == "--vx" || std::string(argv[i]) == "-vx") && i + 1 < argc) {
25 v_x = static_cast<double>(atof(argv[++i]));
26 }
27 else if ((std::string(argv[i]) == "--wz" || std::string(argv[i]) == "-wz") && i + 1 < argc) {
28 w_z = static_cast<double>(atof(argv[++i]));
29 }
30 else if ((std::string(argv[i]) == "--rpm-l" || std::string(argv[i]) == "-rpm-l") && i + 1 < argc) {
31 rpm_command = true;
32 rpm_l = static_cast<double>(atoi(argv[++i]));
33 }
34 else if ((std::string(argv[i]) == "--rpm-r" || std::string(argv[i]) == "-rpm-r") && i + 1 < argc) {
35 rpm_command = true;
36 rpm_r = static_cast<double>(atoi(argv[++i]));
37 }
38 else if (std::string(argv[i]) == "--help" || std::string(argv[i]) == "-h") {
39 std::cout << "Usage: \n"
40 << argv[0]
41 << " [--vx <linear velocity in m/s>]"
42 << " [--wz <rotational velocity in deg/s>]"
43 << " [--rpm-l <motor left RPM>]"
44 << " [--rpm-r <motor right RPM>]"
45 << " [--t <duration of the command in second>]"
46 << " [--help, h]"
47 << std::endl;
48 std::cout << "\nExample:\n" << argv[0] << " --vx 0.05 --wz 0 --t 4\n" << std::endl;
49 return EXIT_SUCCESS;
50 }
51 }
52
53 vpSerial serial("/dev/ttyAMA0", 115200);
54
55 {
56 std::stringstream ss;
57 if (rpm_command) {
58 std::cout << "Apply rpm_l=" << rpm_l << " rpm_r=" << rpm_r << " during " << time << " seconds" << std::endl;
59 ss << "MOTOR_RPM=" << rpm_l << "," << rpm_r << "\n";
60 }
61 else {
62 vpColVector v(2);
63 v[0] = v_x;
64 v[1] = vpMath::rad(w_z);
65 std::cout << "Apply v_x=" << v_x << " m/s "
66 << " w_z=" << w_z << " deg/s during " << time << " seconds" << std::endl;
67 double radius = 0.0325;
68 double L = 0.0725;
69 double motor_left = -(v[0] + L * v[1]) / radius;
70 double motor_right = (v[0] - L * v[1]) / radius;
71 std::cout << "Motor left vel: " << motor_left << " motor right vel: " << motor_right << " (rad/s)" << std::endl;
72 double rpm_left = motor_left * 30. / M_PI;
73 double rpm_right = motor_right * 30. / M_PI;
74
75 ss << "MOTOR_RPM=" << static_cast<int>(rpm_left) << "," << static_cast<int>(rpm_right) << "\n";
76 }
77 std::cout << "Send: " << ss.str() << std::endl;
78 double t0 = vpTime::measureTimeSecond();
79 while (vpTime::measureTimeSecond() - t0 < time) {
80 serial.write(ss.str());
81 vpTime::wait(100);
82 }
83 return EXIT_SUCCESS;
84 }
85 serial.write("MOTOR_RPM=-100,100\n");
86 vpTime::sleepMs(500);
87 serial.write("MOTOR_RPM=-50,100\n");
88 vpTime::sleepMs(500);
89 serial.write("MOTOR_RPM=50,-50\n");
90 vpTime::sleepMs(500);
91 serial.write("LED_RING=0,0,10,0\n");
92 vpTime::sleepMs(500);
93 serial.write("LED_RING=0,0,0,10\n");
94 vpTime::sleepMs(500);
95 serial.write("LED_RING=0,0,0,0\n");
96 vpTime::sleepMs(500);
97 serial.close();
98#else
99 (void)argc;
100 (void)argv;
101 std::cout << "Serial test is only working on unix-like OS." << std::endl;
102#endif
103 return EXIT_SUCCESS;
104}
Implementation of column vector and the associated operations.
static double rad(double deg)
Definition vpMath.h:129
VISP_EXPORT int wait(double t0, double t)
VISP_EXPORT double measureTimeSecond()
VISP_EXPORT void sleepMs(double t)