DCCL v5
Loading...
Searching...
No Matches
test.cpp
1// Copyright 2015-2023:
2// GobySoft, LLC (2013-)
3// Community contributors (see AUTHORS file)
4// File authors:
5// Toby Schneider <toby@gobysoft.org>
6// Stephanie Petillo <stephanie@gobysoft.org>
7//
8//
9// This file is part of the Dynamic Compact Control Language Library
10// ("DCCL").
11//
12// DCCL is free software: you can redistribute it and/or modify
13// it under the terms of the GNU Lesser General Public License as published by
14// the Free Software Foundation, either version 2.1 of the License, or
15// (at your option) any later version.
16//
17// DCCL is distributed in the hope that it will be useful,
18// but WITHOUT ANY WARRANTY; without even the implied warranty of
19// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20// GNU Lesser General Public License for more details.
21//
22// You should have received a copy of the GNU Lesser General Public License
23// along with DCCL. If not, see <http://www.gnu.org/licenses/>.
24#include <iomanip>
25#include <iostream>
26
27#include "dccl/logger.h"
28
29#include "auv_status.pb.h"
30#include "test.pb.h"
31#include <boost/units/base_units/metric/bar.hpp>
32#include <boost/units/io.hpp>
33#include <boost/units/physical_dimensions/pressure.hpp>
34#include <boost/units/systems/si.hpp>
35#include <boost/units/systems/si/dimensionless.hpp>
36#include <boost/units/systems/si/prefixes.hpp>
37#include <boost/units/systems/si/velocity.hpp>
38#include <boost/units/systems/temperature/celsius.hpp>
39#include <boost/units/systems/temperature/fahrenheit.hpp>
40
41#include <boost/units/base_units/metric/nautical_mile.hpp>
42
43#include "dccl/units/conductivity.h"
44
45int main(int argc, char* argv[])
46{
47 bool verbose = false;
48 for (int i = 1; i < argc; ++i)
49 {
50 if (argv[i] && argv[i][0] == '-' && argv[i][1] == 'v' && argv[i][2] == '\0')
51 verbose = true;
52 }
53
54 dccl::dlog.connect(verbose ? dccl::logger::ALL : dccl::logger::WARN_PLUS, &std::cerr);
55
56 CTDTestMessage test_msg;
57
58 using namespace boost::units;
59 using boost::units::metric::bar_base_unit;
60 using boost::units::si::deci;
61
62 typedef bar_base_unit::unit_type Bar;
63 static const Bar bar;
64
65 quantity<Bar> pressure(150.123456789 * si::deci * bar);
66
67 test_msg.set_pressure_with_units(pressure);
68
69 using Kelvin =
70 boost::units::unit<boost::units::temperature_dimension, boost::units::si::system>;
71 quantity<absolute<Kelvin>> temp(15 * absolute<celsius::temperature>());
72 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << temp << std::endl;
73
74 double temp_d = (temp - quantity<absolute<Kelvin>>(0 * absolute<Kelvin>())) / Kelvin();
75 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << temp_d << std::endl;
76
77 test_msg.set_temperature_with_units(15 * absolute<fahrenheit::temperature>());
78 test_msg.set_micro_temp_with_units(15 * Kelvin());
79 test_msg.set_salinity(35.2);
80 test_msg.set_sound_speed(1500);
81
82 quantity<si::velocity> c(1500 * si::meters_per_second);
83 test_msg.set_sound_speed_with_units(c);
84 test_msg.set_depth_with_units(100 * si::meters);
85 quantity<si::velocity> auv_spd(2.5 * si::meters_per_second);
86 test_msg.set_auv_speed_with_units(auv_spd);
87 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "auv_spd: " << auv_spd << std::endl;
88
89 test_msg.set_conductivity_with_units(45.0 * dccl::units::siemens_per_m);
90
91 test_msg.set_salinity_with_units(38.9 * si::dimensionless());
92
93 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << test_msg.DebugString() << std::endl; //outputs protobuf debug string
94 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "Temperature: " << test_msg.temperature_with_units() << std::endl;
95 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "Micro temperature: " << test_msg.micro_temp_with_units() << std::endl;
96 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << std::setprecision(10) << "Pressure: " << test_msg.pressure_with_units()
97 << std::endl;
98 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "Pressure (as bars): " << quantity<Bar>(test_msg.pressure_with_units())
99 << std::endl;
100 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "Sound speed: " << test_msg.sound_speed_with_units() << std::endl;
101 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "AUV speed: " << test_msg.auv_speed_with_units() << std::endl;
102 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "Salinity: " << test_msg.salinity_with_units() << std::endl;
103
104 assert(test_msg.conductivity() == 450000); // uS/cm
105
106 AUVStatus status;
107 status.set_x_with_units(1000 * si::meters);
108 status.set_y_with_units(500 * si::meters);
109 status.set_heading_with_units(3.1415926535 / 2 * si::radians);
110 status.set_heading_rate_with_units(10 * boost::units::degree::degrees /
111 boost::units::si::seconds);
112
113 // Test angular velocity: set in rad/s (SI), read back as rad/s
114 status.set_angular_velocity_with_units(1.0 * si::radians_per_second);
115 assert(std::abs(status.angular_velocity() - 1.0) < 1e-3);
116 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "Angular velocity: " << status.angular_velocity_with_units() << std::endl;
117
118 using NauticalMile = metric::nautical_mile_base_unit::unit_type;
119 quantity<NauticalMile> x_nm(status.x_with_units());
120 quantity<NauticalMile> y_nm(status.y_with_units());
121
122 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << status.DebugString() << std::endl;
123 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << x_nm << std::endl;
124 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << y_nm << std::endl;
125 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << status.heading_with_units() << std::endl;
126 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << status.heading_rate_with_units() << std::endl;
127
128 assert(status.heading_rate() > 9.9999 && status.heading_rate() < 10.0001);
129
130 Parent p;
131 p.set_mass_with_units(2 * si::kilograms);
132 p.set_si_mass_with_units(10 * si::kilograms);
133 p.mutable_child()->set_length_with_units(5 * si::meters);
134
135 assert(p.mass() == 2000); // grams
136 assert(p.si_mass() == 10); // kilograms
137 assert(p.child().length() == 500); // centimeters
138
139 dccl::dlog.is(dccl::logger::INFO) && dccl::dlog << "all tests passed" << std::endl;
140}
bool is(logger::Verbosity verbosity, logger::Group group=logger::GENERAL)
Indicates the verbosity of the Logger until the next std::flush or std::endl. The boolean return is u...
Definition logger.h:191
void connect(int verbosity_mask, Slot slot)
Connect the output of one or more given verbosities to a slot (function pointer or similar)
Definition logger.h:213
Dynamic Compact Control Language namespace.
Definition any.h:28