Closed jschoch closed 2 days ago
rio can not build external hal-component's, in this case, you can add install your hal-component with halcompile and add your hal code to 'postgui_call_list.hal'
but you can use your code inside the quadencoderz plugin or make a new one:
cp -a riocore/plugins/quadencoderz/ riocore/plugins/quadencoderz_jschoch/
and change the plugin.pyy file to something like this (not working / have to fix some variables)
from riocore.plugins import PluginBase
class Plugin(PluginBase):
my_vel_code = """
static uint32_t timestamp = 0;
static uint32_t prev_raw_count = 0;
if( prev_raw_count != value) {
// encoder changed, do some updates
//delta_time = buf->timestamp - timestamp;
delta_time = current_time - timestamp;
//delta_counts = buf->raw_count - cntr->raw_count;
delta_counts = prev_raw_count - value;
timestamp = current_time; // Get timestamp in seconds
// count is usually 1 or -1 since this is wired to the encoder's raw out
// this uses the encoder's ppr to extrapolate revolutions per second
// nanoseconds converted to seconds via 1e-9
vel = delta_counts * (1.0/ (delta_time * 1e-9 * 360));
*(cntr->vel_rpm) = vel * 60;
//rtapi_print_msg(RTAPI_MSG_INFO,
//"vel %f delta_counts: %i delta_time %u period: %i timebase: %i\\n", vel,delta_counts,delta_time,period,timebase);
*(cntr->updates)= *(cntr->updates) + 1;
prev_raw_count = value;
if(cntr->counts_since_timeout < 2) {
cntr->counts_since_timeout++;
} else {
cntr->vel = vel;
}
} else {
// No updates, see if we shoudl timeout
if(cntr->counts_since_timeout) {
delta_time = timebase - timestamp;
if(delta_time < 1e9 / (1 / cntr->scale)) {
// estimate
} else {
cntr->counts_since_timeout = 0;
cntr->vel = 0;
}
} else {
// already timed out;
cntr->vel = 0;
//cntr->vel_rpm = 0;
}
}
// update main timestamp thing
timebase += period;
"""
def setup(self):
self.NAME = "quadencoderz_jschoch"
self.VERILOGS = ["quadencoderz.v"]
self.PINDEFAULTS = {
"a": {
"direction": "input",
"invert": False,
"pullup": False,
},
"b": {
"direction": "input",
"invert": False,
"pullup": False,
},
"z": {
"description": "index pin",
"direction": "input",
"invert": False,
"pullup": False,
},
}
self.OPTIONS = {
"quad_type": {
"default": 2,
"type": int,
"min": 1,
"max": 4,
"description": "encoder type",
},
}
self.INTERFACE = {
"indexenable": {
"size": 1,
"direction": "output",
},
"indexout": {
"size": 1,
"direction": "input",
},
"position": {
"size": 32,
"direction": "input",
},
}
self.SIGNALS = {
"indexenable": {
"is_index_enable": True,
"direction": "inout",
"bool": True,
},
"indexout": {
"is_index_out": True,
"direction": "input",
"bool": True,
},
"position": {
"is_index_position": True,
"direction": "input",
"targets": {
"vel": self.my_vel_code,
"vel_rpm": "value_vel_rpm = value_vel * 60.0;",
},
"description": "position feedback in steps",
},
"rps": {
"direction": "input",
"source": "position",
"description": "calculates revolutions per second",
},
}
self.INFO = "quadencoder with index pin"
self.DESCRIPTION = "usable as spindle-encoder for rigid tapping and thread cutting"
def gateware_instances(self):
instances = self.gateware_instances_base()
instance = instances[self.instances_name]
instance_predefines = instance["predefines"]
instance_parameter = instance["parameter"]
instance_arguments = instance["arguments"]
quad_type = self.plugin_setup.get("quad_type", self.OPTIONS["quad_type"]["default"])
instance_parameter["QUAD_TYPE"] = quad_type
return instances
rename the pluginname in your config, generate and look at rio.c
sorry, forget to rename the signal names:
self.SIGNALS = {
.....
"vel": {
"direction": "input",
"source": "position",
"description": "calculates revolutions per second",
},
"vel_rpm": {
"direction": "input",
"source": "position",
"description": "calculates revolutions per minute",
},
}
Let's say I wanted to integrate some fairly complex module code that also needs it's own hal config. How do I go about integrating that and getting it merged in?
We can use my crappy encoder code, it has a clash on some of the hal pin stuff which makes it an interesting integration example.
Module code
hal code