Motion Master
Loading...
Searching...
No Matches
Macros | Functions
util.cc File Reference
#include "util.h"
#include <sys/stat.h>
#include <bitset>
#include <cmath>
#include <csignal>
#include <fstream>
#include <iomanip>
#include <random>
#include <sstream>
#include "cia402_drive.h"
#include "dictionary_symbols.h"
#include "ethercat_master.h"
#include "ethercat_slave.h"
#include <zip/zip.h>
#include "ethercat.h"
#include "global.h"
#include "loguru.h"
#include "motion_master_error.h"
#include "notifier.h"
#include "virtual_device.h"
Include dependency graph for util.cc:

Macros

#define __le16_to_cpup(x)   (*((uint16_t*)(x)))
 

Functions

std::string get_master_warning_message (MotionMasterWarning warning)
 Return the appropriate warning message. More...
 
std::string get_slave_error_message (int32_t error_code)
 Return the appropriate error message. More...
 
void send (zmq::socket_t &socket, const std::string &connection_id, const motionmaster::MotionMasterMessage &message)
 Send regular Google Protocol Buffers message over ZeroMQ. More...
 
std::string etg1004_unit_string (uint32_t etg1004_unit_value)
 Convert ETG.1004 unit format to string. More...
 
int32_t get_pdo_id (uint16_t index, uint8_t subindex, std::map< int32_t, int32_t > &pdo_position_map)
 Uses a special map and its keys to get the PDO array positions. More...
 
const std::string current_date_time ()
 Get the current time and date as a custom string. More...
 
std::string controllerTypeToString (ControllerType ct)
 
std::string escapeDoubleQuotes (const std::string &input)
 
bool abs_compare (float a, float b)
 Compare absolute values of the input parameters. More...
 
void interruptable_wait (size_t seconds)
 Blocks for the set amount of time, but allows to be interrupted. More...
 
int wrap_target_position (int target, int max_limit, int min_limit)
 
std::vector< double > linspace (double lb, double ub, size_t n)
 Generate equally spaced points inside an interval. More...
 
ControllerGains get_position_controller_gains (Cia402Drive &cia402_drive)
 Get the current position controller gains of a drive. More...
 
bool set_position_controller_gains (Cia402Drive &cia402_drive, const ControllerGains &gains)
 Set the position controller gains. More...
 
bool set_velocity_controller_gains (Cia402Drive &cia402_drive, const ControllerGains &gains)
 Set the velocity controller gains. More...
 
bool check_gains (Cia402Drive &cia402_drive)
 Check if the required gains are properly set. More...
 
bool string_ends_with (const std::string &value, const std::string &ending)
 Check if a string ends with another string. More...
 
std::vector< uint8_t > read_file_content (const std::string &path)
 Read a file into a byte (uint8_t) vector. More...
 
void set_csv_configuration (VirtualDevice *virtual_device, const std::vector< uint8_t > &csv_data)
 Set the configuration parameter values from CSV data. More...
 
uint8_t calculate_crc (const std::vector< uint8_t > &data)
 
uint32_t random_uint32 (uint32_t min, uint32_t max)
 Generate a random unsigned 32-bit integer in a certain range. More...
 
unsigned char random_char ()
 Generate a random character. More...
 
std::string generate_uuid (size_t length)
 Generate a UUID of a custom length. More...
 
int limit_int32_target (int64_t target)
 Limit a target to integer 32 if necessary. More...
 
std::string run_env_info ()
 Get all sorts of run environment information. More...
 
bool validate_sii_data (const std::vector< uint8_t > &content, unsigned int device_id)
 
bool file_exists (const std::string &path)
 Check if a file exists on the system where Motion Master is running. More...
 
std::string string_bytes_to_hex_array_string (const std::string &bytes)
 Convert bytes (as string) to a string of hex values. More...
 
std::string get_firmware_version_string (Cia402Drive &cia402_drive)
 Get the (string) version of the used firmware. More...
 
semver::version get_firmware_version (Cia402Drive &cia402_drive)
 Get the (semver) version of the used firmware. More...
 
bool get_plant_model_from_flash (Cia402Drive &cia402_drive, PlantModel *m)
 Get the plant model from the flash memory of the device. More...
 
AutoTuningDriveConfiguration get_auto_tuning_drive_configuration (Cia402Drive &cia402_drive)
 Get the configuration required for full auto-tuning. More...
 
ControllerGains convert_auto_tuning_controller_gains (ControllerGains gains, uint32_t singleturn_resolution)
 Convert gains to inc pos values and rpm velocity values. More...
 
CirculoType get_circulo_type (int hardware_id)
 
bool is_fw_based_on_v5 (Cia402Drive &cia402_drive)
 Check if the device requires the newer encoder configuration (FW >= v5 or FW >= v0.3.8-calibration), or the older encoder configuration (FW < v5 or FW < v0.3.8-calibration) More...
 
bool is_fw_at_least (Cia402Drive &cia402_drive, const std::string &version)
 Check if the drive uses at least a certain FW version. More...
 
std::vector< uint8_t > zip (const std::string &file_name, std::vector< uint8_t > data)
 Zip a single file. More...
 
std::vector< uint8_t > unzip (const std::vector< uint8_t > &data)
 Unzip data containing a single file. More...
 
uint32_t generate_od_entry_map_key (uint16_t index, uint8_t subindex)
 Generate a map key for an OD entry using index/subindex. More...
 
std::list< std::string > get_file_parts (std::list< std::string > file_list, const std::string &file_name)
 Process the provided file list and find the file or its parts. More...
 
EncoderLocation get_encoder_location (Cia402Drive &cia402_drive, uint8_t encoder_ordinal)
 Get the location of an encoder. More...
 
int get_soem_slaves_responding ()
 
bool compareStringsCaseInsensitive (const std::string &s1, const std::string &s2)
 

Macro Definition Documentation

◆ __le16_to_cpup

#define __le16_to_cpup (   x)    (*((uint16_t*)(x)))

Function Documentation

◆ abs_compare()

bool abs_compare ( float  a,
float  b 
)

Compare absolute values of the input parameters.

Parameters
afirst parameter
bsecond parameter
Returns
a greater than b

◆ calculate_crc()

uint8_t calculate_crc ( const std::vector< uint8_t > &  data)

Calculates the SII checksum field.

The checksum is generated with the polynomial x^8+x^2+x+1 (0x07) and an initial value of 0xff (see IEC 61158-6-12 ch. 5.4).

Parameters
datavector of data
Returns
CRC8

◆ check_gains()

bool check_gains ( Cia402Drive cia402_drive)

Check if the required gains are properly set.

Given the set position control strategy check if the gains have been properly set (and the crucial gains are set to non-zero values)

Parameters
cia402_drive
Returns
true if the gains are correct, false otherwise

◆ compareStringsCaseInsensitive()

bool compareStringsCaseInsensitive ( const std::string &  s1,
const std::string &  s2 
)

◆ controllerTypeToString()

std::string controllerTypeToString ( ControllerType  ct)

◆ convert_auto_tuning_controller_gains()

ControllerGains convert_auto_tuning_controller_gains ( ControllerGains  gains,
uint32_t  singleturn_resolution 
)

Convert gains to inc pos values and rpm velocity values.

Parameters
gainscontroller gains
singleturn_resolutionthe resolution of the used encoder
Returns
converted gains

◆ current_date_time()

const std::string current_date_time ( )

Get the current time and date as a custom string.

Returns
a custom current date and time string

◆ escapeDoubleQuotes()

std::string escapeDoubleQuotes ( const std::string &  input)

◆ etg1004_unit_string()

std::string etg1004_unit_string ( uint32_t  etg1004_unit_value)

Convert ETG.1004 unit format to string.

Parameters
etg1004_unit_value4 byte integer (prefix, numerator, denominator, reserved)
Returns
unit's string (e.g. mNm millinewton meter)

◆ file_exists()

bool file_exists ( const std::string &  path)

Check if a file exists on the system where Motion Master is running.

Parameters
pathpath to the file
Returns
true if exists, false otherwise

◆ generate_od_entry_map_key()

uint32_t generate_od_entry_map_key ( uint16_t  index,
uint8_t  subindex 
)

Generate a map key for an OD entry using index/subindex.

Parameters
indexOD entry index
subindexOD entry subindex
Returns
a uint32 map key for an OD entry

◆ generate_uuid()

std::string generate_uuid ( size_t  length)

Generate a UUID of a custom length.

Parameters
lengthlength of the generated UUID
Returns
a UUID string

◆ get_auto_tuning_drive_configuration()

AutoTuningDriveConfiguration get_auto_tuning_drive_configuration ( Cia402Drive cia402_drive)

Get the configuration required for full auto-tuning.

Fetch all of the parameters required by the full auto-tuning and return them as AutoTuningDriveConfiguration

Parameters
cia402_drive
Returns
AutoTuningDriveConfiguration
Exceptions
MotionMasterError

◆ get_circulo_type()

CirculoType get_circulo_type ( int  hardware_id)

◆ get_encoder_location()

EncoderLocation get_encoder_location ( Cia402Drive cia402_drive,
uint8_t  encoder_ordinal 
)

Get the location of an encoder.

Parameters
cia402_drivea reference to a drive
encoder_ordinalencoder ordinal
Returns
the encoder location
Attention
If the selected encoder is the one used for torque control, then it must be on the motor shaft. This case also covers when this encoder is also being used for position/velocity control.
If the other encoder is selected, the one NOT used for torque control, then it can only be in one of the following locations depending on the presence of a gearbox:
  • no gearbox (gear ratio == 1) - on the motor shaft
  • there is a gearbox present - on the driving shaft

◆ get_file_parts()

std::list< std::string > get_file_parts ( std::list< std::string >  file_list,
const std::string &  file_name 
)

Process the provided file list and find the file or its parts.

Parameters
file_listObtained via do_get_file_list()
file_nameFile name to be found (single file, or its parts)
Returns
a single file name, or its parts if present

◆ get_firmware_version()

semver::version get_firmware_version ( Cia402Drive cia402_drive)

Get the (semver) version of the used firmware.

Parameters
cia402_drive
Returns
semver version object
Exceptions
anexception if there are any errors

◆ get_firmware_version_string()

std::string get_firmware_version_string ( Cia402Drive cia402_drive)

Get the (string) version of the used firmware.

Parameters
cia402_drive
Returns
version as a string
Exceptions
anexception if there are any errors

◆ get_master_warning_message()

std::string get_master_warning_message ( MotionMasterWarning  warning)

Return the appropriate warning message.

Parameters
warningMotionMasterWarning
Returns
warning message

◆ get_pdo_id()

int32_t get_pdo_id ( uint16_t  index,
uint8_t  subindex,
std::map< int32_t, int32_t > &  pdo_position_map 
)

Uses a special map and its keys to get the PDO array positions.

Parameters
indexobject index
subindexobject subindex
Returns
the position of the searched PDO in the array

◆ get_plant_model_from_flash()

bool get_plant_model_from_flash ( Cia402Drive cia402_drive,
PlantModel m 
)

Get the plant model from the flash memory of the device.

Parameters
cia402_driveto get the plant model from
ma pointer to the fetched model if any
Returns
true if exists, false otherwise

◆ get_position_controller_gains()

ControllerGains get_position_controller_gains ( Cia402Drive cia402_drive)

Get the current position controller gains of a drive.

Parameters
cia402_drive
Returns
position controller gains
Exceptions
MotionMasterError(Error::kParameterNotFound);

◆ get_slave_error_message()

std::string get_slave_error_message ( int32_t  error_code)

Return the appropriate error message.

Parameters
error_code
Returns
error message

◆ get_soem_slaves_responding()

int get_soem_slaves_responding ( )

◆ interruptable_wait()

void interruptable_wait ( size_t  seconds)

Blocks for the set amount of time, but allows to be interrupted.

Parameters
secondsamount of time to block for

◆ is_fw_at_least()

bool is_fw_at_least ( Cia402Drive cia402_drive,
const std::string &  version 
)

Check if the drive uses at least a certain FW version.

Parameters
cia402_drivedrive
versionversion as a string
Returns
true if the used FW version is equal or higher than the supplied one

◆ is_fw_based_on_v5()

bool is_fw_based_on_v5 ( Cia402Drive cia402_drive)

Check if the device requires the newer encoder configuration (FW >= v5 or FW >= v0.3.8-calibration), or the older encoder configuration (FW < v5 or FW < v0.3.8-calibration)

Parameters
cia402_drive
Returns
true if the newer encoder configuration is required, false otherwise

◆ limit_int32_target()

int limit_int32_target ( int64_t  target)

Limit a target to integer 32 if necessary.

Parameters
target
Returns
return limited target if necessary

◆ linspace()

std::vector< double > linspace ( double  lb,
double  ub,
size_t  n 
)

Generate equally spaced points inside an interval.

Parameters
lblower boundary
ubupper boundary
nnumber of elements
Returns
a vector of equally spaced double numbers

◆ random_char()

unsigned char random_char ( )

Generate a random character.

Returns
a random character

◆ random_uint32()

uint32_t random_uint32 ( uint32_t  min = 0,
uint32_t  max = UINT32_MAX 
)

Generate a random unsigned 32-bit integer in a certain range.

Parameters
minminimum value
maxmaximum value
Returns
a random unsigned 32-bit integer

◆ read_file_content()

std::vector< uint8_t > read_file_content ( const std::string &  path)

Read a file into a byte (uint8_t) vector.

Parameters
pathfile path
Returns
vector of bytes if successful, empty vector otherwise

◆ run_env_info()

std::string run_env_info ( )

Get all sorts of run environment information.

Returns
a string with run environment information

◆ send()

void send ( zmq::socket_t &  socket,
const std::string &  connection_id,
const motionmaster::MotionMasterMessage &  message 
)

Send regular Google Protocol Buffers message over ZeroMQ.

Parameters
socketZeroMQ socket used for sending messages
connection_idconnection ID as a string
messageGoogle Protocol Buffers message to be sent

◆ set_csv_configuration()

void set_csv_configuration ( VirtualDevice virtual_device,
const std::vector< uint8_t > &  csv_data 
)

Set the configuration parameter values from CSV data.

Parameters
virtual_devicepointer to the device to set the configuration on
csv_dataCSV data

◆ set_position_controller_gains()

bool set_position_controller_gains ( Cia402Drive cia402_drive,
const ControllerGains gains 
)

Set the position controller gains.

Parameters
cia402_drive
gains
Returns
true if successful, false otherwise

◆ set_velocity_controller_gains()

bool set_velocity_controller_gains ( Cia402Drive cia402_drive,
const ControllerGains gains 
)

Set the velocity controller gains.

Parameters
cia402_drive
gains
Returns
true if successful, false otherwise

◆ string_bytes_to_hex_array_string()

std::string string_bytes_to_hex_array_string ( const std::string &  bytes)

Convert bytes (as string) to a string of hex values.

Parameters
bytesbytes as a string
Returns
string of hex bytes

◆ string_ends_with()

bool string_ends_with ( const std::string &  value,
const std::string &  ending 
)

Check if a string ends with another string.

Parameters
valuestring to check in
endingstring to check for
Returns
true if found, false otherwise

◆ unzip()

std::vector< uint8_t > unzip ( const std::vector< uint8_t > &  data)

Unzip data containing a single file.

Parameters
data
Returns
unzipped data
Exceptions
std::runtime_error

◆ validate_sii_data()

bool validate_sii_data ( const std::vector< uint8_t > &  content,
unsigned int  device_id 
)

◆ wrap_target_position()

int wrap_target_position ( int  target,
int  max_limit,
int  min_limit 
)

◆ zip()

std::vector< uint8_t > zip ( const std::string &  file_name,
std::vector< uint8_t >  data 
)

Zip a single file.

Parameters
file_name
data
Returns
zipped data