Skip to content

Commit

Permalink
aylp_units helper functions and pid units param
Browse files Browse the repository at this point in the history
  • Loading branch information
imyxh committed Jul 27, 2024
1 parent 8ab7fdb commit e8d5475
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 21 deletions.
6 changes: 5 additions & 1 deletion devices/pid.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ int pid_init(struct aylp_device *self)
data->type = AYLP_T_MATRIX;
else log_error("Unrecognized type: %s", s);
log_trace("type = %s (0x%hhX)", s, data->type);
} else if (!strcmp(key, "units")) {
const char *s = json_object_get_string(val);
data->units = aylp_units_from_string(s);
log_trace("units = %s (0x%hhX)", s, data->units);
} else if (!strcmp(key, "p")) {
data->p = json_object_get_double(val);
log_trace("p = %G", data->p);
Expand Down Expand Up @@ -85,7 +89,7 @@ int pid_init(struct aylp_device *self)
self->type_in = data->type;
self->units_in = AYLP_U_ANY;
self->type_out = 0;
self->units_out = 0;
self->units_out = data->units;
return 0;
}

Expand Down
2 changes: 2 additions & 0 deletions devices/pid.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
struct aylp_pid_data {
// param type in ["vector", "matrix"]
aylp_type type;
// units to output
aylp_units units;
// accumulated error
union {
gsl_vector *acc_v;
Expand Down
2 changes: 2 additions & 0 deletions doc/devices/pid.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ Parameters
- `type` (string) (required)
- "vector" if we are expecting `T_VECTOR` input, and "matrix" if we are
expecting `T_MATRIX` input.
- `units` (string) (optional)
- Output units, e.g. "V", "rad", etc. Defaults to null (unchanged from input).
- `p` (float) (optional)
- Coefficent for proportional correction (see equation above). Defaults
to 1.0.
Expand Down
47 changes: 31 additions & 16 deletions libaylp/anyloop.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,9 @@ static void cleanup(void)

aylp_type aylp_type_from_string(const char *type_name)
{
if (!strcasecmp(type_name, "none")) return AYLP_T_NONE;
if (!strcasecmp(type_name, "block")) return AYLP_T_BLOCK;
if (!strcasecmp(type_name, "vector")) return AYLP_T_VECTOR;
if (!strcasecmp(type_name, "matrix")) return AYLP_T_MATRIX;
if (!strcasecmp(type_name, "block_uchar")) return AYLP_T_BLOCK_UCHAR;
if (!strcasecmp(type_name, "matrix_uchar")) return AYLP_T_MATRIX_UCHAR;
#define AYLP_TYPE_FROM_STRING_MATCH_TYPE(TYPE, type) \
if (!strcasecmp(type_name, #type)) return TYPE;
FOR_AYLP_TYPES(AYLP_TYPE_FROM_STRING_MATCH_TYPE)

log_error("Couldn't parse type: %s", type_name);
return AYLP_T_NONE;
Expand All @@ -65,22 +62,40 @@ aylp_type aylp_type_from_string(const char *type_name)
const char *aylp_type_to_string(aylp_type type)
{
switch (type) {
case AYLP_T_BLOCK:
return "block";
case AYLP_T_VECTOR:
return "vector";
case AYLP_T_MATRIX:
return "matrix";
case AYLP_T_BLOCK_UCHAR:
return "block_uchar";
case AYLP_T_MATRIX_UCHAR:
return "matrix_uchar";
#define AYLP_TYPE_TO_STRING_MATCH_TYPE(TYPE, type) \
case TYPE: \
return #type; \
break;
FOR_AYLP_TYPES(AYLP_TYPE_TO_STRING_MATCH_TYPE)
default:
log_error("Unknown type 0x%hhX", type);
return "NONE";
}
}

aylp_units aylp_units_from_string(const char *units_name)
{
#define AYLP_UNITS_FROM_STRING_MATCH_UNITS(UNITS, units) \
if (!strcasecmp(units_name, #units)) return UNITS;
FOR_AYLP_UNITS(AYLP_UNITS_FROM_STRING_MATCH_UNITS)

log_error("Couldn't parse units: %s", units_name);
return AYLP_U_NONE;
}

const char *aylp_units_to_string(aylp_units units)
{
switch (units) {
#define AYLP_UNITS_TO_STRING_MATCH_UNITS(UNITS, units) \
case UNITS: \
return #units; \
break;
FOR_AYLP_UNITS(AYLP_UNITS_TO_STRING_MATCH_UNITS)
default:
log_error("Unknown units 0x%hhX", units);
return "NONE";
}
}


static void handle_signal(int sig, siginfo_t *info, void *context)
Expand Down
36 changes: 32 additions & 4 deletions libaylp/anyloop.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,15 @@ enum {
AYLP_T_ANY = 0xFF,
// add more as necessary
};
// https://en.wikipedia.org/wiki/X_macro
#define FOR_AYLP_TYPES(DO) \
DO(AYLP_T_NONE, none) \
DO(AYLP_T_BLOCK, block) \
DO(AYLP_T_VECTOR, vector) \
DO(AYLP_T_MATRIX, matrix) \
DO(AYLP_T_BLOCK_UCHAR, block_uchar) \
DO(AYLP_T_MATRIX_UCHAR, matrix_uchar) \
DO(AYLP_T_ANY, any)


/** Enum for units of data in aylp_state.
Expand All @@ -99,6 +108,13 @@ enum {
AYLP_U_ANY = 0xFF,
// add more as necessary
};
#define FOR_AYLP_UNITS(DO) \
DO(AYLP_U_NONE, none) \
DO(AYLP_U_COUNTS, counts) \
DO(AYLP_U_MINMAX, minmax) \
DO(AYLP_U_RAD, rad) \
DO(AYLP_U_V, V) \
DO(AYLP_U_ANY, any)


/** Little-endian representation of "AYLP" as a magic number. */
Expand Down Expand Up @@ -233,17 +249,29 @@ struct aylp_conf {
};


/** Convert a string to a type.
* @param type_name: the string holding the type name (e.g. "vector")
/** Convert a string to a pure type. Case insensitive.
* @param type_name: the string holding the type name (e.g. "vector").
*/
aylp_type aylp_type_from_string(const char *type_name);


/** Convert a type to its name as a string.
* @param type: the string holding the type name (e.g. "vector")
/** Convert a pure type to its name as a string. Returns lowercase.
* @param type: the type to convert (e.g. AYLP_T_VECTOR).
*/
const char *aylp_type_to_string(aylp_type type);


/** Convert a string to the pure units it represents. Case insensitive.
* @param units_name: the string holding the units (e.g. "rad").
*/
aylp_units aylp_units_from_string(const char *units_name);


/** Convert pure units to their name as a string. Mixed case (e.g. rad, V).
* @param units: the units to convert (e.g. AYLP_U_RAD).
*/
const char *aylp_units_to_string(aylp_units units);


#endif // include guard

0 comments on commit e8d5475

Please sign in to comment.