Skip to content

Commit

Permalink
Poll analog inputs to drive a configured endpoint.
Browse files Browse the repository at this point in the history
  • Loading branch information
kalvdans authored and Christian Häggström committed Sep 13, 2018
1 parent fb5cae8 commit cc0f083
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 3 deletions.
31 changes: 30 additions & 1 deletion Firmware/MotorControl/low_level.cpp
Expand Up @@ -724,4 +724,33 @@ void pwm_in_cb(int channel, uint32_t timestamp) {
last_timestamp[gpio_num - 1] = timestamp;
last_pin_state[gpio_num - 1] = current_pin_state;
last_sample_valid[gpio_num - 1] = true;
}
}


/* Analog speed control input */

static void update_analog_endpoint(const struct PWMMapping_t *map, int gpio)
{
float fraction = get_adc_voltage(get_gpio_port_by_pin(gpio), get_gpio_pin_by_pin(gpio)) / 3.3f;
float value = map->min + (fraction * (map->max - map->min));
get_endpoint(map->endpoint)->set_from_float(value);
}

static void analog_polling_thread(void *)
{
while (true) {
for (int i = 0; i < GPIO_COUNT; i++) {
struct PWMMapping_t *map = &board_config.analog_mappings[i];

if (is_endpoint_ref_valid(map->endpoint))
update_analog_endpoint(map, i + 1);
}
osDelay(200);
}
}

void start_analog_thread()
{
osThreadDef(thread_def, analog_polling_thread, osPriorityLow, 0, 4*512);
osThreadCreate(osThread(thread_def), NULL);
}
2 changes: 2 additions & 0 deletions Firmware/MotorControl/low_level.h
Expand Up @@ -53,6 +53,8 @@ void pwm_in_init();

void update_brake_current();

void start_analog_thread();

#ifdef __cplusplus
}
#endif
Expand Down
2 changes: 2 additions & 0 deletions Firmware/MotorControl/main.cpp
Expand Up @@ -208,6 +208,8 @@ int odrive_main(void) {
axes[i]->start_thread();
}

start_analog_thread();

system_stats_.fully_booted = true;
return 0;
}
1 change: 1 addition & 0 deletions Firmware/MotorControl/odrive_main.h
Expand Up @@ -75,6 +75,7 @@ struct BoardConfig_t {
//<! the brake power if the brake resistor is disabled.
//<! The default is 26V for the 24V board version and 52V for the 48V board version.
PWMMapping_t pwm_mappings[GPIO_COUNT];
PWMMapping_t analog_mappings[GPIO_COUNT];
};
extern BoardConfig_t board_config;
extern bool user_config_loaded_;
Expand Down
7 changes: 5 additions & 2 deletions Firmware/communication/communication.cpp
Expand Up @@ -163,8 +163,11 @@ static inline auto make_obj_tree() {
make_protocol_object("gpio2_pwm_mapping", make_protocol_definitions(board_config.pwm_mappings[1])),
make_protocol_object("gpio3_pwm_mapping", make_protocol_definitions(board_config.pwm_mappings[2])),
#endif
make_protocol_object("gpio4_pwm_mapping", make_protocol_definitions(board_config.pwm_mappings[3]))
),
make_protocol_object("gpio4_pwm_mapping", make_protocol_definitions(board_config.pwm_mappings[3])),

make_protocol_object("gpio3_analog_mapping", make_protocol_definitions(board_config.analog_mappings[2])),
make_protocol_object("gpio4_analog_mapping", make_protocol_definitions(board_config.analog_mappings[3]))
),
make_protocol_object("axis0", axes[0]->make_protocol_definitions()),
make_protocol_object("axis1", axes[1]->make_protocol_definitions()),
make_protocol_object("can", can1_ctx.make_protocol_definitions()),
Expand Down

0 comments on commit cc0f083

Please sign in to comment.