|
4 | 4 |
|
5 | 5 | #include <viam/sdk/common/instance.hpp>
|
6 | 6 | #include <viam/sdk/components/camera.hpp>
|
| 7 | +#include <viam/sdk/log/logging.hpp> |
7 | 8 | #include <viam/sdk/robot/client.hpp>
|
8 | 9 | #include <viam/sdk/rpc/dial.hpp>
|
9 | 10 |
|
10 |
| -int main() { |
11 |
| - using std::cerr; |
12 |
| - using std::cout; |
13 |
| - using std::endl; |
| 11 | +int main() try { |
14 | 12 | namespace vs = ::viam::sdk;
|
15 |
| - try { |
16 |
| - // Every Viam C++ SDK program must have one and only one Instance object which is created |
17 |
| - // before |
18 |
| - // any other C++ SDK objects and stays alive until all Viam C++ SDK objects are destroyed. |
19 |
| - vs::Instance inst; |
20 |
| - |
21 |
| - // If you want to connect to a remote robot, this should be the url of the robot |
22 |
| - // Ex: xxx.xxx.viam.cloud |
23 |
| - std::string robot_address("localhost:8080"); |
24 |
| - // If you want to connect to a remote robot, you need some authentication secret |
25 |
| - // You can find this on app.viam.com |
26 |
| - vs::Credentials credentials("", ""); |
27 |
| - |
28 |
| - vs::DialOptions dial_options; |
29 |
| - |
30 |
| - // If you have credentials, use this to pass them to the robot |
31 |
| - // dial_options.credentials = credentials; |
32 |
| - |
33 |
| - // This is for an example. Care should be taken before exercising this option in production. |
34 |
| - dial_options.set_allow_insecure_downgrade( |
35 |
| - (credentials.type().empty() && credentials.payload().empty())); |
36 |
| - |
37 |
| - // Set the refresh interval of the robot (in seconds) (0 = auto refresh) and the dial |
38 |
| - // options |
39 |
| - vs::Options options = vs::Options(1, dial_options); |
40 |
| - |
41 |
| - std::shared_ptr<vs::RobotClient> robot; |
42 |
| - try { |
43 |
| - robot = vs::RobotClient::at_address(robot_address, options); |
44 |
| - cout << "Successfully connected to the robot" << endl; |
45 |
| - } catch (const std::exception& e) { |
46 |
| - cerr << "Failed to connect to the robot. Exiting." << endl; |
47 |
| - throw; |
48 |
| - } |
49 |
| - |
50 |
| - std::vector<vs::Name> resource_names = robot->resource_names(); |
51 |
| - |
52 |
| - cout << "Resources of the robot:" << endl; |
53 |
| - for (const auto& resource : resource_names) { |
54 |
| - cout << " - " << resource.name() << " (" << resource.api().resource_subtype() << ")" |
55 |
| - << endl; |
56 |
| - } |
57 |
| - |
58 |
| - std::string camera_name("camera1"); |
59 |
| - |
60 |
| - cout << "Getting camera: " << camera_name << endl; |
61 |
| - std::shared_ptr<vs::Camera> camera; |
62 |
| - try { |
63 |
| - camera = robot->resource_by_name<vs::Camera>(camera_name); |
64 |
| - } catch (const std::exception& e) { |
65 |
| - cerr << "Failed to find " << camera_name << ". Exiting." << endl; |
66 |
| - throw; |
67 |
| - } |
68 |
| - vs::Camera::properties props = camera->get_properties(); |
69 |
| - vs::Camera::intrinsic_parameters intrinsics = props.intrinsic_parameters; |
70 |
| - cout << "Image dimensions: " << intrinsics.width_px << " x " << intrinsics.height_px |
71 |
| - << endl; |
72 |
| - |
73 |
| - std::string output_file("img.png"); |
74 |
| - std::string image_mime_type("image/png"); |
75 |
| - |
76 |
| - cout << "Getting image from camera " << endl; |
77 |
| - vs::Camera::raw_image img = camera->get_image(image_mime_type); |
78 |
| - cout << "Got image of mime type: " << img.mime_type << endl; |
79 |
| - |
80 |
| - cout << "Getting and saving image to " << output_file << endl; |
81 |
| - std::ofstream fout; |
82 |
| - fout.open(output_file, std::ios::binary | std::ios::out); |
83 |
| - if (fout.fail()) { |
84 |
| - throw std::runtime_error("Failed to open output file " + output_file); |
85 |
| - } |
86 |
| - fout.write(reinterpret_cast<char*>(img.bytes.data()), img.bytes.size()); |
87 |
| - fout.close(); |
88 |
| - if (fout.fail()) { |
89 |
| - throw std::runtime_error("Failed to write and close output file " + output_file); |
90 |
| - } |
91 |
| - } catch (const std::exception& ex) { |
92 |
| - cerr << "Program failed. Exception: " << std::string(ex.what()) << endl; |
93 |
| - return EXIT_FAILURE; |
94 |
| - } catch (...) { |
95 |
| - cerr << "Program failed without exception message." << endl; |
96 |
| - return EXIT_FAILURE; |
| 13 | + // Every Viam C++ SDK program must have one and only one Instance object which is created |
| 14 | + // before |
| 15 | + // any other C++ SDK objects and stays alive until all Viam C++ SDK objects are destroyed. |
| 16 | + vs::Instance inst; |
| 17 | + |
| 18 | + // If you want to connect to a remote robot, this should be the url of the robot |
| 19 | + // Ex: xxx.xxx.viam.cloud |
| 20 | + std::string robot_address("localhost:8080"); |
| 21 | + // If you want to connect to a remote robot, you need some authentication secret |
| 22 | + // You can find this on app.viam.com |
| 23 | + vs::Credentials credentials("", ""); |
| 24 | + |
| 25 | + vs::DialOptions dial_options; |
| 26 | + |
| 27 | + // If you have credentials, use this to pass them to the robot |
| 28 | + // dial_options.credentials = credentials; |
| 29 | + |
| 30 | + // This is for an example. Care should be taken before exercising this option in production. |
| 31 | + dial_options.set_allow_insecure_downgrade( |
| 32 | + (credentials.type().empty() && credentials.payload().empty())); |
| 33 | + |
| 34 | + // Set the refresh interval of the robot (in seconds) (0 = auto refresh) and the dial |
| 35 | + // options |
| 36 | + vs::Options options = vs::Options(1, dial_options); |
| 37 | + |
| 38 | + std::shared_ptr<vs::RobotClient> robot = vs::RobotClient::at_address(robot_address, options); |
| 39 | + VIAM_SDK_LOG(info) << "Successfully connected to the robot"; |
| 40 | + |
| 41 | + std::vector<vs::Name> resource_names = robot->resource_names(); |
| 42 | + |
| 43 | + VIAM_SDK_LOG(info) << "Resources of the robot:"; |
| 44 | + for (const auto& resource : resource_names) { |
| 45 | + VIAM_SDK_LOG(info) << " - " << resource.name() << " (" << resource.api().resource_subtype() |
| 46 | + << ")"; |
97 | 47 | }
|
98 | 48 |
|
| 49 | + std::string camera_name("camera1"); |
| 50 | + |
| 51 | + VIAM_SDK_LOG(info) << "Getting camera: " << camera_name; |
| 52 | + std::shared_ptr<vs::Camera> camera = robot->resource_by_name<vs::Camera>(camera_name); |
| 53 | + |
| 54 | + vs::Camera::properties props = camera->get_properties(); |
| 55 | + vs::Camera::intrinsic_parameters intrinsics = props.intrinsic_parameters; |
| 56 | + VIAM_SDK_LOG(info) << "Image dimensions: " << intrinsics.width_px << " x " |
| 57 | + << intrinsics.height_px; |
| 58 | + |
| 59 | + std::string output_file("img.png"); |
| 60 | + std::string image_mime_type("image/png"); |
| 61 | + |
| 62 | + VIAM_SDK_LOG(info) << "Getting image from camera "; |
| 63 | + |
| 64 | + vs::Camera::raw_image img = camera->get_image(image_mime_type); |
| 65 | + |
| 66 | + VIAM_SDK_LOG(info) << "Got image of mime type: " << img.mime_type; |
| 67 | + |
| 68 | + VIAM_SDK_LOG(info) << "Getting and saving image to " << output_file; |
| 69 | + |
| 70 | + std::ofstream fout; |
| 71 | + |
| 72 | + // Operations on the ofstream will throw on failure. |
| 73 | + fout.exceptions(std::ofstream::failbit); |
| 74 | + fout.open(output_file, std::ios::binary | std::ios::out); |
| 75 | + |
| 76 | + fout.write(reinterpret_cast<char*>(img.bytes.data()), img.bytes.size()); |
| 77 | + fout.close(); |
| 78 | + |
99 | 79 | return EXIT_SUCCESS;
|
| 80 | +} catch (const std::exception& ex) { |
| 81 | + std::cerr << "Program failed. Exception: " << std::string(ex.what()) << "\n"; |
| 82 | + return EXIT_FAILURE; |
100 | 83 | }
|
0 commit comments