2016-05-25 17:37:40 +02:00
|
|
|
# Wrapper around C helper code
|
|
|
|
#
|
2018-07-18 17:51:28 +02:00
|
|
|
# Copyright (C) 2016-2018 Kevin O'Connor <kevin@koconnor.net>
|
2016-05-25 17:37:40 +02:00
|
|
|
#
|
|
|
|
# This file may be distributed under the terms of the GNU GPLv3 license.
|
|
|
|
import os, logging
|
|
|
|
import cffi
|
|
|
|
|
2017-04-13 18:09:37 +02:00
|
|
|
|
|
|
|
######################################################################
|
|
|
|
# c_helper.so compiling
|
|
|
|
######################################################################
|
|
|
|
|
2018-06-06 03:40:32 +02:00
|
|
|
COMPILE_CMD = ("gcc -Wall -g -O2 -shared -fPIC"
|
|
|
|
" -flto -fwhole-program -fno-use-linker-plugin"
|
|
|
|
" -o %s %s")
|
2018-06-06 22:49:44 +02:00
|
|
|
SOURCE_FILES = [
|
2019-10-27 18:23:03 +01:00
|
|
|
'pyhelper.c', 'serialqueue.c', 'stepcompress.c', 'itersolve.c', 'trapq.c',
|
2018-08-23 20:21:58 +02:00
|
|
|
'kin_cartesian.c', 'kin_corexy.c', 'kin_delta.c', 'kin_polar.c',
|
2018-07-13 05:28:58 +02:00
|
|
|
'kin_winch.c', 'kin_extruder.c',
|
2018-06-06 22:49:44 +02:00
|
|
|
]
|
2016-05-25 17:37:40 +02:00
|
|
|
DEST_LIB = "c_helper.so"
|
2018-06-09 01:55:24 +02:00
|
|
|
OTHER_FILES = [
|
2019-10-27 18:23:03 +01:00
|
|
|
'list.h', 'serialqueue.h', 'stepcompress.h', 'itersolve.h', 'pyhelper.h',
|
|
|
|
'trapq.h',
|
2018-06-09 01:55:24 +02:00
|
|
|
]
|
2016-05-25 17:37:40 +02:00
|
|
|
|
|
|
|
defs_stepcompress = """
|
2018-06-08 23:02:20 +02:00
|
|
|
struct stepcompress *stepcompress_alloc(uint32_t oid);
|
|
|
|
void stepcompress_fill(struct stepcompress *sc, uint32_t max_error
|
|
|
|
, uint32_t invert_sdir, uint32_t queue_step_msgid
|
|
|
|
, uint32_t set_next_step_dir_msgid);
|
2016-11-30 07:58:45 +01:00
|
|
|
void stepcompress_free(struct stepcompress *sc);
|
2017-04-05 01:37:54 +02:00
|
|
|
int stepcompress_reset(struct stepcompress *sc, uint64_t last_step_clock);
|
2019-02-27 19:20:57 +01:00
|
|
|
int stepcompress_queue_msg(struct stepcompress *sc
|
|
|
|
, uint32_t *data, int len);
|
2017-04-05 01:37:54 +02:00
|
|
|
|
2018-06-06 22:49:44 +02:00
|
|
|
struct steppersync *steppersync_alloc(struct serialqueue *sq
|
|
|
|
, struct stepcompress **sc_list, int sc_num, int move_num);
|
|
|
|
void steppersync_free(struct steppersync *ss);
|
|
|
|
void steppersync_set_time(struct steppersync *ss
|
|
|
|
, double time_offset, double mcu_freq);
|
|
|
|
int steppersync_flush(struct steppersync *ss, uint64_t move_clock);
|
|
|
|
"""
|
|
|
|
|
2018-06-09 01:55:24 +02:00
|
|
|
defs_itersolve = """
|
2019-10-28 02:05:57 +01:00
|
|
|
int32_t itersolve_generate_steps(struct stepper_kinematics *sk
|
|
|
|
, double flush_time);
|
|
|
|
double itersolve_check_active(struct stepper_kinematics *sk
|
|
|
|
, double flush_time);
|
|
|
|
void itersolve_set_trapq(struct stepper_kinematics *sk, struct trapq *tq);
|
2018-06-09 01:55:24 +02:00
|
|
|
void itersolve_set_stepcompress(struct stepper_kinematics *sk
|
|
|
|
, struct stepcompress *sc, double step_dist);
|
2019-11-14 00:58:51 +01:00
|
|
|
void itersolve_set_position(struct stepper_kinematics *sk
|
2018-06-22 19:03:07 +02:00
|
|
|
, double x, double y, double z);
|
2018-06-14 22:33:57 +02:00
|
|
|
double itersolve_get_commanded_pos(struct stepper_kinematics *sk);
|
2018-06-09 01:55:24 +02:00
|
|
|
"""
|
|
|
|
|
2019-10-27 18:23:03 +01:00
|
|
|
defs_trapq = """
|
2019-10-29 17:44:39 +01:00
|
|
|
void trapq_append(struct trapq *tq, double print_time
|
2019-10-27 18:23:03 +01:00
|
|
|
, double accel_t, double cruise_t, double decel_t
|
|
|
|
, double start_pos_x, double start_pos_y, double start_pos_z
|
2019-11-06 14:41:50 +01:00
|
|
|
, double axes_r_x, double axes_r_y, double axes_r_z
|
2019-10-27 18:23:03 +01:00
|
|
|
, double start_v, double cruise_v, double accel);
|
2019-10-27 23:50:53 +01:00
|
|
|
struct trapq *trapq_alloc(void);
|
|
|
|
void trapq_free(struct trapq *tq);
|
|
|
|
void trapq_free_moves(struct trapq *tq, double print_time);
|
2019-10-27 18:23:03 +01:00
|
|
|
"""
|
|
|
|
|
2018-06-06 22:49:44 +02:00
|
|
|
defs_kin_cartesian = """
|
2018-06-09 20:30:22 +02:00
|
|
|
struct stepper_kinematics *cartesian_stepper_alloc(char axis);
|
|
|
|
"""
|
|
|
|
|
|
|
|
defs_kin_corexy = """
|
|
|
|
struct stepper_kinematics *corexy_stepper_alloc(char type);
|
2018-06-06 22:49:44 +02:00
|
|
|
"""
|
|
|
|
|
|
|
|
defs_kin_delta = """
|
2018-06-09 03:30:38 +02:00
|
|
|
struct stepper_kinematics *delta_stepper_alloc(double arm2
|
|
|
|
, double tower_x, double tower_y);
|
2016-05-25 17:37:40 +02:00
|
|
|
"""
|
|
|
|
|
2018-08-23 20:21:58 +02:00
|
|
|
defs_kin_polar = """
|
|
|
|
struct stepper_kinematics *polar_stepper_alloc(char type);
|
|
|
|
"""
|
|
|
|
|
2018-07-13 05:28:58 +02:00
|
|
|
defs_kin_winch = """
|
|
|
|
struct stepper_kinematics *winch_stepper_alloc(double anchor_x
|
|
|
|
, double anchor_y, double anchor_z);
|
|
|
|
"""
|
|
|
|
|
2018-06-13 22:53:25 +02:00
|
|
|
defs_kin_extruder = """
|
|
|
|
struct stepper_kinematics *extruder_stepper_alloc(void);
|
2019-11-05 22:21:06 +01:00
|
|
|
void extruder_set_pressure(struct stepper_kinematics *sk
|
|
|
|
, double pressure_advance, double half_smooth_time);
|
2018-06-13 22:53:25 +02:00
|
|
|
"""
|
|
|
|
|
2016-05-25 17:37:40 +02:00
|
|
|
defs_serialqueue = """
|
|
|
|
#define MESSAGE_MAX 64
|
|
|
|
struct pull_queue_message {
|
|
|
|
uint8_t msg[MESSAGE_MAX];
|
|
|
|
int len;
|
|
|
|
double sent_time, receive_time;
|
|
|
|
};
|
|
|
|
|
2016-07-11 17:41:49 +02:00
|
|
|
struct serialqueue *serialqueue_alloc(int serial_fd, int write_only);
|
2016-05-25 17:37:40 +02:00
|
|
|
void serialqueue_exit(struct serialqueue *sq);
|
2016-11-29 22:22:54 +01:00
|
|
|
void serialqueue_free(struct serialqueue *sq);
|
2016-05-25 17:37:40 +02:00
|
|
|
struct command_queue *serialqueue_alloc_commandqueue(void);
|
2016-11-30 07:58:45 +01:00
|
|
|
void serialqueue_free_commandqueue(struct command_queue *cq);
|
2016-05-25 17:37:40 +02:00
|
|
|
void serialqueue_send(struct serialqueue *sq, struct command_queue *cq
|
|
|
|
, uint8_t *msg, int len, uint64_t min_clock, uint64_t req_clock);
|
2018-05-28 15:42:59 +02:00
|
|
|
void serialqueue_pull(struct serialqueue *sq
|
|
|
|
, struct pull_queue_message *pqm);
|
2019-02-27 19:20:57 +01:00
|
|
|
void serialqueue_set_baud_adjust(struct serialqueue *sq
|
|
|
|
, double baud_adjust);
|
2018-05-28 15:42:59 +02:00
|
|
|
void serialqueue_set_receive_window(struct serialqueue *sq
|
|
|
|
, int receive_window);
|
2017-06-28 02:08:18 +02:00
|
|
|
void serialqueue_set_clock_est(struct serialqueue *sq, double est_freq
|
|
|
|
, double last_clock_time, uint64_t last_clock);
|
2016-05-25 17:37:40 +02:00
|
|
|
void serialqueue_get_stats(struct serialqueue *sq, char *buf, int len);
|
|
|
|
int serialqueue_extract_old(struct serialqueue *sq, int sentq
|
|
|
|
, struct pull_queue_message *q, int max);
|
|
|
|
"""
|
|
|
|
|
2016-11-30 18:04:28 +01:00
|
|
|
defs_pyhelper = """
|
|
|
|
void set_python_logging_callback(void (*func)(const char *));
|
2017-02-06 19:31:34 +01:00
|
|
|
double get_monotonic(void);
|
2016-11-30 18:04:28 +01:00
|
|
|
"""
|
|
|
|
|
2018-06-09 01:55:24 +02:00
|
|
|
defs_std = """
|
|
|
|
void free(void*);
|
|
|
|
"""
|
|
|
|
|
2018-06-06 22:49:44 +02:00
|
|
|
defs_all = [
|
2019-02-27 19:20:57 +01:00
|
|
|
defs_pyhelper, defs_serialqueue, defs_std,
|
2019-10-27 18:23:03 +01:00
|
|
|
defs_stepcompress, defs_itersolve, defs_trapq,
|
2018-08-23 20:21:58 +02:00
|
|
|
defs_kin_cartesian, defs_kin_corexy, defs_kin_delta, defs_kin_polar,
|
2018-07-13 05:28:58 +02:00
|
|
|
defs_kin_winch, defs_kin_extruder
|
2018-06-06 22:49:44 +02:00
|
|
|
]
|
|
|
|
|
2016-05-25 17:37:40 +02:00
|
|
|
# Return the list of file modification times
|
|
|
|
def get_mtimes(srcdir, filelist):
|
|
|
|
out = []
|
|
|
|
for filename in filelist:
|
|
|
|
pathname = os.path.join(srcdir, filename)
|
|
|
|
try:
|
|
|
|
t = os.path.getmtime(pathname)
|
|
|
|
except os.error:
|
|
|
|
continue
|
|
|
|
out.append(t)
|
|
|
|
return out
|
|
|
|
|
|
|
|
# Check if the code needs to be compiled
|
2017-04-13 18:09:37 +02:00
|
|
|
def check_build_code(srcdir, target, sources, cmd, other_files=[]):
|
|
|
|
src_times = get_mtimes(srcdir, sources + other_files)
|
|
|
|
obj_times = get_mtimes(srcdir, [target])
|
2016-05-25 17:37:40 +02:00
|
|
|
if not obj_times or max(src_times) > min(obj_times):
|
2017-09-27 17:43:14 +02:00
|
|
|
logging.info("Building C code module %s", target)
|
2017-04-13 18:09:37 +02:00
|
|
|
srcfiles = [os.path.join(srcdir, fname) for fname in sources]
|
|
|
|
destlib = os.path.join(srcdir, target)
|
2019-11-07 17:32:20 +01:00
|
|
|
res = os.system(cmd % (destlib, ' '.join(srcfiles)))
|
|
|
|
if res:
|
|
|
|
msg = "Unable to build C code module (error=%s)" % (res,)
|
|
|
|
logging.error(msg)
|
|
|
|
raise Exception(msg)
|
2016-05-25 17:37:40 +02:00
|
|
|
|
|
|
|
FFI_main = None
|
|
|
|
FFI_lib = None
|
2016-11-30 18:04:28 +01:00
|
|
|
pyhelper_logging_callback = None
|
2016-05-25 17:37:40 +02:00
|
|
|
|
|
|
|
# Return the Foreign Function Interface api to the caller
|
|
|
|
def get_ffi():
|
2016-11-30 18:04:28 +01:00
|
|
|
global FFI_main, FFI_lib, pyhelper_logging_callback
|
2016-05-25 17:37:40 +02:00
|
|
|
if FFI_lib is None:
|
|
|
|
srcdir = os.path.dirname(os.path.realpath(__file__))
|
2017-04-13 18:09:37 +02:00
|
|
|
check_build_code(srcdir, DEST_LIB, SOURCE_FILES, COMPILE_CMD
|
|
|
|
, OTHER_FILES)
|
2016-05-25 17:37:40 +02:00
|
|
|
FFI_main = cffi.FFI()
|
2018-06-06 22:49:44 +02:00
|
|
|
for d in defs_all:
|
|
|
|
FFI_main.cdef(d)
|
2016-05-25 17:37:40 +02:00
|
|
|
FFI_lib = FFI_main.dlopen(os.path.join(srcdir, DEST_LIB))
|
2016-11-30 18:04:28 +01:00
|
|
|
# Setup error logging
|
|
|
|
def logging_callback(msg):
|
|
|
|
logging.error(FFI_main.string(msg))
|
|
|
|
pyhelper_logging_callback = FFI_main.callback(
|
|
|
|
"void(const char *)", logging_callback)
|
|
|
|
FFI_lib.set_python_logging_callback(pyhelper_logging_callback)
|
2016-05-25 17:37:40 +02:00
|
|
|
return FFI_main, FFI_lib
|
2017-04-13 18:09:37 +02:00
|
|
|
|
|
|
|
|
|
|
|
######################################################################
|
|
|
|
# hub-ctrl hub power controller
|
|
|
|
######################################################################
|
|
|
|
|
|
|
|
HC_COMPILE_CMD = "gcc -Wall -g -O2 -o %s %s -lusb"
|
|
|
|
HC_SOURCE_FILES = ['hub-ctrl.c']
|
2018-05-05 15:54:54 +02:00
|
|
|
HC_SOURCE_DIR = '../../lib/hub-ctrl'
|
2017-04-13 18:09:37 +02:00
|
|
|
HC_TARGET = "hub-ctrl"
|
|
|
|
HC_CMD = "sudo %s/hub-ctrl -h 0 -P 2 -p %d"
|
|
|
|
|
|
|
|
def run_hub_ctrl(enable_power):
|
|
|
|
srcdir = os.path.dirname(os.path.realpath(__file__))
|
|
|
|
hubdir = os.path.join(srcdir, HC_SOURCE_DIR)
|
|
|
|
check_build_code(hubdir, HC_TARGET, HC_SOURCE_FILES, HC_COMPILE_CMD)
|
|
|
|
os.system(HC_CMD % (hubdir, enable_power))
|
2018-10-16 18:21:18 +02:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
get_ffi()
|