21 const class error_category :
public std::error_category {
23 using std::error_category::error_category;
24 virtual const char* name()
const noexcept override {
25 return "gclib::error::category";
27 virtual std::string message(
int result)
const override {
30 return "An invalid argument was passed";
32 return "An internal error occurred";
34 return "The operation timed out";
36 return "The controller returned a question mark";
38 return "The data could not fit in the provided buffer";
40 return "The controller or gcaps is not connected";
42 return "Not subscribed to unsolicited data";
44 return "Unrecognized gclib result";
49 class error :
public std::system_error {
51 using std::system_error::system_error;
64 const size_t HUGE_BUFFER = 524288;
70 throw error(std::error_code(
static_cast<enum result
>(result), error_category));
72 throw error(std::error_code(
static_cast<enum result
>(result), error_category), err);
76 template <
typename ...Args>
77 inline std::string wrap(std::function<
gclib_result(
char*,
size_t, Args...)> func, Args... args) {
78 char* huge_buffer =
static_cast<char*
>(std::malloc(
sizeof(
char) * HUGE_BUFFER));
79 check(func(huge_buffer,
sizeof(
char) * HUGE_BUFFER, args...));
80 std::string result(huge_buffer);
81 std::free(huge_buffer);
230 std::string result(1024,
'\0');
232 return std::vector<uint8_t>(result.begin(), result.end());
320 if (connection_type_ ==
Serial) {
344 template <
typename T = std::
string>
347 if constexpr (std::is_integral<T>() || std::is_floating_point<T>()) {
348 return std::stod(str);
356 std::string
array(std::string name,
size_t start = 0,
size_t end = 0) {
return wrap({[name](
gclib_handle h,
char* buf,
size_t len,
size_t start,
size_t end){
return gclib_array(h, name.c_str(), buf, len, start, end); }}, start, end); }
421 message_callback_ = std::function([callback](
const char*
message){ callback(
message); });
519 progress_callback_ = callback;
539 std::string address_;
540 size_t baud_rate_{115200};
543 internal::check(result, h_);
545 template <
typename ...Args>
547 char* huge_buffer =
static_cast<char*
>(std::malloc(
sizeof(
char) * internal::HUGE_BUFFER));
548 check(func(h_, huge_buffer,
sizeof(
char) * internal::HUGE_BUFFER, args...));
549 std::string result(huge_buffer);
550 std::free(huge_buffer);
553 template <
typename T,
typename ...Args>
556 check(func(h_, &result, args...));
561 static inline void wrap_message_callback(
void* context,
const char*
message){ (*(std::function<void(
const char*)>*)(context))(
message); }
564 static inline void wrap_progress_callback(
void* context,
size_t current,
size_t max){ (*(std::function<void(
size_t,
size_t)>*)(context))(current, max); }
565 std::function<void(
const char*)> message_callback_;
568 std::function<void(
size_t,
size_t)> progress_callback_;
void subscribe_messages(std::function< void(std::string message)> callback)
Subscribe to unsolicited messages.
void subscribe_interrupts()
Subscribe to interrupts.
void set_firmware(std::string file_path)
std::string address()
Returns the address that was used to connect to the controller.
Interrupt interrupt(int timeout=-1)
void unsubscribe_progress()
std::string array(std::string name, size_t start=0, size_t end=0)
void subscribe_data_records(std::function< void(DataRecord data_record)> callback)
Subscribe to data records.
void set_interrupts(Interrupt::Type mask, std::bitset< 8 > motion_complete_axes=0, std::bitset< 8 > digital_inputs=0)
Configure which interrupts will be generated by the controller.
void unsubscribe_data_records()
ConnectionType connection_type()
Returns the type of address that was used to connect to the controller.
Controller(std::string address, size_t baud_rate)
Open a serial connection to a controller reported by addresses().
void subscribe_messages()
Subscribe to unsolicited messages.
std::string revision_information()
Returns controller ^R^V output.
void subscribe_progress(std::function< void(size_t current, size_t max)> callback)
Register a progress callback.
std::string message(int timeout=-1)
DataRecord data_record(int timeout=-1)
Controller(std::string address)
Open a connection to a controller reported by addresses().
void open()
Open the connection. If connection is already open, it will be closed and reopened.
void unsubscribe_messages()
void subscribe_interrupts(std::function< void(Interrupt interrupt)> callback)
Subscribe to interrupts.
void subscribe_data_records()
Subscribe to data records.
void set_program(std::string program, std::string insert="")
void set_data_records(size_t period_ms)
Configure controller data records.
void set_array(std::string name, std::string array, size_t start=0, size_t end=0)
T command(std::string command)
Issues a command to the controller and provides the response.
void unsubscribe_interrupts()
Thrown when the underlying C library returns a nonzero gclib_result.
bool gclib_data_record_electronic_lock_out(gclib_data_record_handle h, size_t amp)
Returns electronic lockout (ELO) status for a bank.
bool gclib_data_record_over_voltage(gclib_data_record_handle h, size_t amp)
Returns over voltage status.
bool gclib_data_record_over_temp(gclib_data_record_handle h, size_t amp)
Returns over temperature status.
bool gclib_data_record_under_voltage(gclib_data_record_handle h, size_t amp)
Returns under voltage status.
bool gclib_data_record_over_current(gclib_data_record_handle h, size_t amp)
Returns over current status.
int32_t gclib_data_record_variable(gclib_data_record_handle h, char axis)
Returns the value of a user variable for an axis.
int32_t gclib_data_record_reference_position(gclib_data_record_handle h, char axis)
Returns the reference position.
bool gclib_data_record_reverse_limit(gclib_data_record_handle h, char axis)
Returns the state of the reverse limit.
int32_t gclib_data_record_velocity(gclib_data_record_handle h, char axis)
Returns the velocity.
int gclib_data_record_home_phase(gclib_data_record_handle h, char axis)
Returns home phase when in GCLIB_HOME mode.
bool gclib_data_record_moving(gclib_data_record_handle h, char axis)
Returns true if motion is being profiled.
bool gclib_data_record_stopping(gclib_data_record_handle h, char axis)
Returns true if axis is stopping.
bool gclib_data_record_forward_limit(gclib_data_record_handle h, char axis)
Returns the state of the forward limit.
gclib_mode_of_motion_t gclib_data_record_mode_of_motion(gclib_data_record_handle h, char axis)
Returns the mode of motion being profiled.
bool gclib_data_record_hall_error(gclib_data_record_handle h, char axis)
Returns true if the amplifier for this axis has triggered a hall error.
int32_t gclib_data_record_aux_position(gclib_data_record_handle h, char axis)
Returns the auxiliary position.
bool gclib_data_record_slewing(gclib_data_record_handle h, char axis)
Returns true if axis is slewing.
int32_t gclib_data_record_position_error(gclib_data_record_handle h, char axis)
Returns the position error.
bool gclib_data_record_home_input(gclib_data_record_handle h, char axis)
Returns the state of the home input.
bool gclib_data_record_final_deceleration(gclib_data_record_handle h, char axis)
Returns true when axis is in final deceleration.
uint8_t gclib_data_record_stop_code(gclib_data_record_handle h, char axis)
Returns a code indicating why the motor has stopped. See the SC command reference for details.
double gclib_data_record_analog_input(gclib_data_record_handle h, char axis)
Returns the analog input.
bool gclib_data_record_latch_armed(gclib_data_record_handle h, char axis)
Get latch armed.
bool gclib_data_record_latch_occurred(gclib_data_record_handle h, char axis)
Returns whether the latch was triggered.
bool gclib_data_record_latch_input(gclib_data_record_handle h, char axis)
Returns the state of the latch input.
bool gclib_data_record_negative_direction_move(gclib_data_record_handle h, char axis)
Returns true if axis velocity is negative.
int32_t gclib_data_record_position(gclib_data_record_handle h, char axis)
Returns the position.
bool gclib_data_record_motor_off(gclib_data_record_handle h, char axis)
Get motor off status.
double gclib_data_record_torque(gclib_data_record_handle h, char axis)
Returns the torque.
bool gclib_data_record_stepper_mode(gclib_data_record_handle h, char axis)
Returns true if axis is configured as a stepper motor.
uint8_t gclib_data_record_halls(gclib_data_record_handle h, char axis)
Returns the hall state.
bool gclib_data_record_peak_current(gclib_data_record_handle h, char axis)
Returns true if the amplifier for this axis has triggered a peak current error.
gclib_result gclib_close(gclib_handle *h)
Close connection to controller.
gclib_result gclib_assign_ip(const char *mac, const char *ip)
Assign IP to a controller.
gclib_result gclib_address(gclib_handle h, char *address, size_t len)
Get connection address.
gclib_result gclib_set_baud_rate(gclib_handle h, size_t baud_rate)
Set baud rate of serial connection.
gclib_result gclib_ip_requests(char *ip_requests, size_t len)
Show MAC address, model, and serial of all controllers requesting IP addresses, comma-separated....
gclib_result gclib_addresses(char *addresses, size_t len)
Show address, model, and serial of detected controllers, where available. Detected controllers are ne...
const char * gclib_error(gclib_handle h)
Get library error string.
gclib_result gclib_open(gclib_handle *h, const char *address)
Open a connection to a controller.
void gclib_force_gcaps(bool on)
Force future library calls to use either gcaps or direct connections.
gclib_result gclib_connection_type(gclib_handle h, gclib_connection_t *type)
Get connection type.
gclib_result gclib_command(gclib_handle h, const char *command, char *buf, size_t len)
Issues a command to the controller and provides the response.
gclib_result gclib_serial_number(gclib_handle h, uint32_t *serial_number)
Get serial number from controller.
gclib_result gclib_revision_information(gclib_handle h, char *rev_info, size_t len)
Get revision information (^R^V) from controller.
gclib_result gclib_published(char *name, size_t len)
Provides published status of local gcaps server.
gclib_result gclib_set_server(const char *name)
Set gcaps server.
gclib_result gclib_list_servers(char *servers, size_t len)
List available gcaps servers separated by newline.
gclib_result gclib_server(char *name, size_t len)
Current gcaps server.
gclib_result gclib_set_published(const char *name)
Set published status of local gcaps server.
gclib_result gclib_set_program(gclib_handle h, const char *program, const char *insert)
Set the program on the controller.
gclib_result gclib_set_array(gclib_handle h, const char *name, const char *buf, size_t start, size_t end)
Set an array on the controller.
gclib_result gclib_array(gclib_handle h, const char *name, char *buf, size_t len, size_t start, size_t end)
Get an array from the controller.
gclib_result gclib_set_firmware(gclib_handle h, const char *file_path)
Set the firmware on the controller.
gclib_result gclib_program(gclib_handle h, char *program, size_t len)
Get the controller's current program.
gclib_result gclib_interrupt(gclib_handle h, gclib_interrupt_t *interrupt, int timeout)
Get a queued interrupt, or wait up to timeout ms for one to arrive.
gclib_result gclib_message(gclib_handle h, char *message, size_t len, int timeout)
Get a queued unsolicited message, or wait up to timeout ms for one to arrive.
gclib_result gclib_subscribe_data_records(gclib_handle h, void(callback)(void *user_data, gclib_data_record_handle data_record), void *user_data)
Subscribe to data records.
gclib_result gclib_set_interrupts(gclib_handle h, gclib_interrupt_type interrupt_mask, gclib_axis_flags motion_complete_axes, gclib_digital_input_flags digital_inputs)
Configure which interrupts will be generated by the controller.
gclib_result gclib_subscribe_interrupts(gclib_handle h, void(callback)(void *user_data, gclib_interrupt_t interrupt), void *user_data)
Subscribe to interrupts.
gclib_result gclib_unsubscribe(gclib_handle h, void *callback)
Unsubscribe from messages, interrupts, data records, or progress.
gclib_result gclib_set_data_records(gclib_handle h, size_t period_ms)
Configure controller data records.
gclib_result gclib_subscribe_progress(gclib_handle h, void(callback)(void *user_data, size_t current, size_t max), void *user_data)
Subscribe to progress for gclib_set_program(), gclib_set_array(), and gclib_set_firmware().
gclib_result gclib_subscribe_messages(gclib_handle h, void(callback)(void *user_data, const char *message), void *user_data)
Subscribe to unsolicited messages.
gclib_result gclib_data_record(gclib_handle h, gclib_data_record_handle *data_record, int timeout)
Get a queued data record, or wait up to timeout ms for one to arrive.
void * gclib_data_record_handle
A handle to a data record, for use in Data Record API calls.
gclib_digital_input_flags
Flags to enable or disable GCLIB_DIGITAL_INPUT_LOW interrupts per digital input, for use in gclib_set...
gclib_axis_flags
Flags for building an axis mask, for example in gclib_set_interrupts().
gclib_result gclib_gcaps_version(char *gcaps_version, size_t len)
Get library version used by current gcaps server.
gclib_result gclib_version(char *version, size_t len)
Get library version.
struct gclib_context * gclib_handle
A handle to an open connection given by gclib_open() for use in future API calls.
struct gclib_interrupt_t gclib_interrupt_t
An interrupt generated by the controller.
@ GCLIB_PROGRAM_STOPPED
If multiple threads are running, this interrupt is only triggered when all threads are finished.
@ GCLIB_EXCESS_POSITION_ERROR
Must be reenabled with gclib_set_interrupts() after occurrence.
@ GCLIB_ALL_AXES_MOTION_COMPLETE
@ GCLIB_LIMIT_SWITCH
Must be reenabled with gclib_set_interrupts() after each occurrence.
@ GCLIB_DIGITAL_INPUT_LOW
Must be reenabled with gclib_set_interrupts() after each occurrence.
bool gclib_data_record_coordinated_move_slewing(gclib_data_record_handle h, char plane)
Returns true if slewing.
uint16_t gclib_data_record_coordinated_move_status(gclib_data_record_handle h, char plane)
Returns the coordinated move status word.
bool gclib_data_record_coordinated_move_stopping(gclib_data_record_handle h, char plane)
Returns true if stopping.
uint16_t gclib_data_record_coordinated_move_segment_count(gclib_data_record_handle h, char plane)
Returns the segment count for the current move.
uint16_t gclib_data_record_coordinated_move_buffer_available(gclib_data_record_handle h, char plane)
Returns the available buffer space.
int32_t gclib_data_record_coordinated_move_distance(gclib_data_record_handle h, char plane)
Returns the distance covered in the current move.
bool gclib_data_record_coordinated_move_moving(gclib_data_record_handle h, char plane)
Returns true if moving.
bool gclib_data_record_coordinated_move_final_deceleration(gclib_data_record_handle h, char plane)
Returns true if in final deceleration.
uint8_t gclib_data_record_input_bank(gclib_data_record_handle h, size_t index)
Get a bank of inputs.
uint8_t gclib_data_record_error_code(gclib_data_record_handle h)
Get the error code.
gclib_ethernet_status_t gclib_data_record_ethernet_status(gclib_data_record_handle h, char handle)
Get ethernet handle status.
gclib_result gclib_data_record_bytes(gclib_data_record_handle h, char *data_record, size_t len)
Copy data record to user buffer.
bool gclib_data_record_output(gclib_data_record_handle h, size_t index)
Get an output.
uint32_t gclib_data_record_contour_segment_count(gclib_data_record_handle h)
Get the contour segment count.
bool gclib_data_record_thread_running(gclib_data_record_handle h, size_t thread)
Returns true if a given thread is running.
bool gclib_data_record_input(gclib_data_record_handle h, size_t index)
Get an input.
uint16_t gclib_data_record_contour_buffer_available(gclib_data_record_handle h)
Get remaining contour buffer.
uint8_t gclib_data_record_output_bank(gclib_data_record_handle h, size_t index)
Get a bank of outputs.
uint16_t gclib_data_record_sample(gclib_data_record_handle h)
The sample in which this data record was generated.
@ GCLIB_POSITION_RELATIVE
@ GCLIB_POSITION_ABSOLUTE
void set_published(std::string name={})
Publish local gcaps server as name, or unpublish if name is empty.
void set_server(std::string name={})
std::string published()
Return name of local gcaps server, or an empty string if unpublished.
std::string ip_requests()
Show MAC address, model, and serial of all controllers requesting IP addresses. Detected controllers ...
ConnectionType
Returned by Controller::connection_type().
std::string list_servers()
List available gcaps servers separated by newline.
std::string server()
Current gcaps server, or an empty string if connected to the local gcaps server.
std::string version()
Installed gclib version.
std::string gcaps_version()
Installed gcaps version, as reported by current gcaps server.
void force_gcaps(bool on)
Force future library calls to use either gcaps or direct connections.
void assign_ip(std::string mac, std::string ip)
std::string addresses()
Show address, model, and serial of detected controllers, where available. Detected controllers are ne...
Holds amplifier-specific data record fields. Provided by DataRecord.amp().
Amp(gclib_data_record_handle h, size_t amp)
bool electronic_lock_out()
Holds axis-specific data record fields. Provided by DataRecord.axis().
ModeOfMotion mode_of_motion()
int32_t reference_position()
bool final_deceleration()
Axis(gclib_data_record_handle h, char axis)
bool negative_direction_move()
Holds coordinated move specific data record fields. Provided by DataRecord.coordinated_move().
bool final_deceleration()
uint16_t buffer_available()
CoordinatedMove(gclib_data_record_handle h, char plane)
CoordinatedMove coordinated_move(char plane)
Get an instance of DataRecord.CoordinatedMove, which provides CM-specific data record fields.
Axis axis(char axis)
Get an instance of DataRecord.Axis, which provides axis-specific data record fields.
EthernetStatus ethernet_status(char handle)
std::bitset< 8 > input_bank(size_t index)
uint32_t contour_segment_count()
bool thread_running(size_t thread)
uint16_t contour_buffer_available()
std::vector< uint8_t > bytes()
Get data record as raw bytes.
Amp amp(size_t amp)
Get an instance of DataRecord.Amp, which provides amplifier-specific data record fields.
DataRecord(gclib_data_record_handle h)
std::bitset< 8 > output_bank(size_t index)
bool output(size_t index)
@ ProgramStopped
If multiple threads are running, this interrupt is only triggered when all threads are finished.
@ DigitalInputLow
Must be reenabled with set_interrupts() after each occurrence.
@ LimitSwitch
Must be reenabled with set_interrupts() after each occurrence.
@ ExcessPositionError
Must be reenabled with set_interrupts() after occurrence.
uint8_t digital_input
If type is DigitalInputLow, holds the digital input that triggered the interrupt.
uint8_t user_interrupt
If type is UserInterrupt, holds the user interrupt that was triggered.
Interrupt(gclib_interrupt_t interrupt)
char axis
If type is MotionComplete, holds the axis that triggered the interrupt.
uint8_t status
The raw status byte generated by the controller. See the EI command reference for all possible values...
An interrupt generated by the controller.