Skip to content

Commit

Permalink
Merge pull request #264 from zivid/2024-07-30-update-cpp-samples
Browse files Browse the repository at this point in the history
Samples: Add AutomaticNetworkConfigurationForCameras
  • Loading branch information
SatjaSivcev authored Jul 30, 2024
2 parents 80bbf46 + 973536a commit ae8df0f
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 0 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ from the camera can be used.
- [MultiCameraCaptureSequentiallyWithInterleavedProcessing](https://github.com/zivid/zivid-cpp-samples/tree/master/source/Camera/Advanced/MultiCameraCaptureSequentiallyWithInterleavedProcessing/MultiCameraCaptureSequentiallyWithInterleavedProcessing.cpp) - Capture point clouds with multiple cameras sequentially
with interleaved processing.
- **InfoUtilOther**
- [AutomaticNetworkConfigurationForCameras](https://github.com/zivid/zivid-cpp-samples/tree/master/source/Camera/InfoUtilOther/AutomaticNetworkConfigurationForCameras/AutomaticNetworkConfigurationForCameras.cpp) - Automatically set the IP addresses of any number of
cameras to be in the same subnet as the provided IP address
of the network interface.
- [CameraInfo](https://github.com/zivid/zivid-cpp-samples/tree/master/source/Camera/InfoUtilOther/CameraInfo/CameraInfo.cpp) - List connected cameras and print camera version and state
information for each connected camera.
- [CameraUserData](https://github.com/zivid/zivid-cpp-samples/tree/master/source/Camera/InfoUtilOther/CameraUserData/CameraUserData.cpp) - Store user data on the Zivid camera.
Expand Down
2 changes: 2 additions & 0 deletions source/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ set(SAMPLES
Camera/Advanced/AllocateMemoryForPointCloudData
Camera/Advanced/CaptureHalconViaGenICam
Camera/Advanced/CaptureHalconViaZivid
Camera/InfoUtilOther/AutomaticNetworkConfigurationForCameras
Camera/InfoUtilOther/CameraUserData
Camera/InfoUtilOther/CaptureWithDiagnostics
Camera/InfoUtilOther/GetCameraIntrinsics
Expand Down Expand Up @@ -120,6 +121,7 @@ set(Visualization_DEPENDING
ROIBoxViaCheckerboard
)
set(Clipp_DEPENDING
AutomaticNetworkConfigurationForCameras
CameraUserData
MultiCameraCalibrationFromZDF
StitchByTransformation
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/*
Automatically set the IP addresses of any number of cameras to be in the same subnet as the provided IP address of the network interface.
*/

#include <Zivid/Zivid.h>
#include <clipp.h>
#include <iostream>
#include <stdexcept>
#include <string>
#include <vector>

namespace
{
void assertUserInput(const std::string &ipAddress, const std::string &subnetMask)
{
(void)Zivid::NetworkConfiguration::IPV4::Address{ ipAddress };
(void)Zivid::NetworkConfiguration::IPV4::SubnetMask{ subnetMask };
}

std::vector<std::string> splitUserInput(const std::string &str, char delimiter)
{
std::vector<std::string> parts;
std::istringstream userInput(str);
for(std::string part; std::getline(userInput, part, delimiter);)
{
parts.push_back(part);
}
return parts;
}

std::tuple<std::string, std::string> parseOptions(int argc, char **argv)
{
std::string ipAddress;
std::string subnetMask = "255.255.255.0";

auto cli = clipp::group(
clipp::required("--interface-ipv4") & clipp::value("IP address of the PC network interface", ipAddress),
clipp::option("--subnet-mask") & clipp::value("Network subnet mask (default: 255.255.255.0)", subnetMask));

if(!clipp::parse(argc, argv, cli))
{
auto fmt = clipp::doc_formatting{}.alternatives_min_split_size(1).surround_labels("\"", "\"");
std::cout << "SYNOPSIS:" << std::endl;
std::cout << clipp::usage_lines(cli, argv[0], fmt) << std::endl;
std::cout << "OPTIONS:" << std::endl;
std::cout << clipp::documentation(cli) << std::endl;
throw std::runtime_error("Command-line parsing failed");
}

assertUserInput(ipAddress, subnetMask);

return std::make_tuple(ipAddress, subnetMask);
}
} // namespace

int main(int argc, char **argv)
{
try
{
std::tuple<std::string, std::string> options = parseOptions(argc, argv);
std::string ipAddress = std::get<0>(options);
std::string subnetMask = std::get<1>(options);

assertUserInput(ipAddress, subnetMask);

auto ipAddressOctets = splitUserInput(ipAddress, '.');

Zivid::Application zivid;
auto cameras = zivid.cameras();
if(cameras.empty())
{
throw std::runtime_error("Failed to connect to camera. No cameras found.");
}

int lastIpAddressOctet = std::stoi(ipAddressOctets[3]);
std::string remainingIpAddressOctets = ipAddressOctets[0] + "." + ipAddressOctets[1] + "." + ipAddressOctets[2];

// defines the last octet of the ip address of the first Zivid camera. Eg.: x.x.x.2
int nextIpAddressLastOctet = 2;

for(auto &camera : cameras)
{
if(nextIpAddressLastOctet == lastIpAddressOctet)
{
nextIpAddressLastOctet += 1;
}

Zivid::NetworkConfiguration newConfig(Zivid::NetworkConfiguration::IPV4(
Zivid::NetworkConfiguration::IPV4::Mode::manual,
Zivid::NetworkConfiguration::IPV4::Address(
remainingIpAddressOctets + "." + std::to_string(nextIpAddressLastOctet)),
Zivid::NetworkConfiguration::IPV4::SubnetMask(subnetMask)));

nextIpAddressLastOctet += 1;

std::cout << "Applying network configuration to camera " << camera.info().serialNumber() << std::endl;
camera.applyNetworkConfiguration(newConfig);
std::cout << "New " << camera.networkConfiguration() << "\n" << std::endl;
}
}
catch(const std::exception &e)
{
std::cerr << "Error: " << e.what() << std::endl;
std::cout << "Press enter to exit." << std::endl;
std::cin.get();
return EXIT_FAILURE;
}

return EXIT_SUCCESS;
}

0 comments on commit ae8df0f

Please sign in to comment.