DCCL v3
test.cpp
1 // Copyright 2009-2017 Toby Schneider (http://gobysoft.org/index.wt/people/toby)
2 // GobySoft, LLC (for 2013-)
3 // Massachusetts Institute of Technology (for 2007-2014)
4 // Community contributors (see AUTHORS file)
5 //
6 //
7 // This file is part of the Dynamic Compact Control Language Library
8 // ("DCCL").
9 //
10 // DCCL is free software: you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as published by
12 // the Free Software Foundation, either version 2.1 of the License, or
13 // (at your option) any later version.
14 //
15 // DCCL is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public License
21 // along with DCCL. If not, see <http://www.gnu.org/licenses/>.
22 #include <iostream>
23 #include <iomanip>
24 
25 #include <boost/units/systems/si/prefixes.hpp>
26 #include <boost/units/base_units/metric/bar.hpp>
27 #include <boost/units/physical_dimensions/pressure.hpp>
28 #include <boost/units/io.hpp>
29 #include <boost/units/systems/temperature/celsius.hpp>
30 #include <boost/units/systems/temperature/fahrenheit.hpp>
31 #include <boost/units/systems/si/velocity.hpp>
32 #include <boost/units/systems/si.hpp>
33 #include <boost/units/systems/si/dimensionless.hpp>
34 #include "test.pb.h"
35 #include "auv_status.pb.h"
36 
37 #include <boost/units/base_units/metric/nautical_mile.hpp>
38 
39 int main()
40 {
41  CTDTestMessage test_msg;
42 
43  using namespace boost::units;
44  using boost::units::metric::bar_base_unit;
45  using boost::units::si::deci;
46 
47  typedef bar_base_unit::unit_type Bar;
48  static const Bar bar;
49 
50 
51  quantity<Bar> pressure(150.123456789*si::deci*bar);
52 
53  test_msg.set_pressure_with_units(pressure);
54 
55  typedef boost::units::unit<boost::units::temperature_dimension,boost::units::si::system> Kelvin;
56  quantity<absolute<Kelvin> > temp(15*absolute<celsius::temperature>());
57  std::cout << temp << std::endl;
58 
59  double temp_d = (temp - quantity<absolute<Kelvin> >(0*absolute<Kelvin>()))/Kelvin();
60  std::cout << temp_d << std::endl;
61 
62  test_msg.set_temperature_with_units(15*absolute<fahrenheit::temperature>());
63  test_msg.set_micro_temp_with_units(15*Kelvin());
64  test_msg.set_salinity(35.2);
65  test_msg.set_sound_speed(1500);
66 
67  quantity<si::velocity> c(1500*si::meters_per_second);
68  test_msg.set_sound_speed_with_units(c);
69  test_msg.set_depth_with_units(100*si::meters);
70  quantity<si::velocity> auv_spd(2.5*si::meters_per_second);
71  test_msg.set_auv_speed_with_units(auv_spd);
72  std::cout <<"auv_spd: " <<auv_spd <<std::endl;
73 
74  test_msg.set_salinity_with_units(38.9*si::dimensionless());
75 
76 
77  std::cout << test_msg.DebugString() << std::endl; //outputs protobuf debug string
78  std::cout << "Temperature: " << test_msg.temperature_with_units() << std::endl;
79  std::cout << "Micro temperature: " << test_msg.micro_temp_with_units() << std::endl;
80  std::cout <<std::setprecision(10) << "Pressure: " << test_msg.pressure_with_units() << std::endl;
81  std::cout << "Pressure (as bars): " << quantity<Bar>(test_msg.pressure_with_units()) << std::endl;
82  std::cout << "Sound speed: " << test_msg.sound_speed_with_units() << std::endl;
83  std::cout << "AUV speed: " << test_msg.auv_speed_with_units() << std::endl;
84  std::cout << "Salinity: " << test_msg.salinity_with_units() << std::endl;
85 
86  AUVStatus status;
87  status.set_x_with_units(1000*si::meters);
88  status.set_y_with_units(500*si::meters);
89  status.set_heading_with_units(3.1415926535/2*si::radians);
90 
91  typedef metric::nautical_mile_base_unit::unit_type NauticalMile;
92  quantity<NauticalMile> x_nm(status.x_with_units());
93  quantity<NauticalMile> y_nm(status.y_with_units());
94 
95  std::cout << status.DebugString() << std::endl;
96  std::cout << x_nm << std::endl;
97  std::cout << y_nm << std::endl;
98  std::cout << status.heading_with_units() << std::endl;
99 
100 
101  Parent p;
102  p.set_mass_with_units(2*si::kilograms);
103  p.set_si_mass_with_units(10*si::kilograms);
104  p.mutable_child()->set_length_with_units(5*si::meters);
105 
106  assert(p.mass() == 2000); // grams
107  assert(p.si_mass() == 10); // kilograms
108  assert(p.child().length() == 500); // centimeters
109 
110  std::cout << "all tests passed" << std::endl;
111 }