Page MenuHomec4science

motordriver.h
No OneTemporary

File Metadata

Created
Thu, Nov 28, 05:41

motordriver.h

/*
* File: motordriver.h
* Author: tuleu
*
* Created on August 22, 2012, 3:43 PM
*/
#ifndef MOTORDRIVER_H_
#define MOTORDRIVER_H_
#include <gpio.h>
#include <register.h>
#include "pwm.h"
// number of counts per rotation
#define ANGULAR_RESOLUTION 4096
// maximum torque output value
#define TORQUE_RESOLUTION 4096
// spring force scaling: for a given SPRING_FORCE, maximum torque output is reached after TORQUE_PER_ROTATION_SCALING/SPRING_FORCE rotations
#define TORQUE_PER_ROTATION_SCALING 16
// since we have really low resolution speed
// in order to improve movement resolution, we multiply by a multiple of two
// the speed. this is the power of the multiplication. aim is to improve the precision
#define SPEED_BINARY_PRECISION 4
// common constants and macro's
#define MAX_PID_RANGE 32767
// communication protocol parameters
// Maximum counter value of HW PWM counter
#define MAX_PWM (FCY/FPWM-1)
#define MAX_DUTY (2*FCY/FPWM-1)
///\todo these macro as parameters
#define MDV_INERTIA_COMP 300
#define MDV_SLOWFAST_COMP 200
#define MDV_SLOWFAST_THRES 220
#define SMOOTH_POSITION_OVERSHOOT 0
enum mdv_status {
MDV_IDLE = 0,
MDV_MOVING = 1 << 0,
MDV_RUNTIME_CALIBRATED = 1 << 1,
MDV_ERROR = 1 << 2
};
typedef enum mdv_status mdv_status;
typedef enum mdv_control_mode {
MDV_M_COAST = 0,
MDV_M_BRAKE = 1,
MDV_M_POSITION_CONTROL = 2,
MDV_M_VELOCITY_CONTROL = 3,
MDV_M_SMOOTH_POSITION_CONTROL = 4,
MDV_M_TORQUE_CONTROL = 5,
MDV_M_RUNTIME_CALIBRATION = 6,
MDV_M_SINE_GENERATOR = 7,
MDV_NUMBER_OF_CONTROL_MODE
} mdv_control_mode;
union mdv_status_and_control{
struct {
uint8_t mode;
uint8_t status;
};
unsigned int uint;
sbcp_reg_val reg_val;
};
typedef union mdv_status_and_control mdv_status_and_control;
typedef unsigned int mdv_pid_t;
typedef unsigned int mdv_stiffness_t;
typedef unsigned int mdv_damping_t;
typedef unsigned int mdv_timestep_t;
typedef int mdv_torque_t;
struct mdv_parameters {
mdv_pid_t p_gain;
mdv_pid_t i_gain;
mdv_pid_t d_gain;
mdv_stiffness_t stiffness;
mdv_damping_t damping;
mdv_torque_t preload;
mdv_timestep_t smooth_interval;
};
typedef struct mdv_parameters mdv_parameters;
struct mdv_pin {
gpio brake;
gpio coast;
gpio dir;
gpio mode;
pwm_t torque;
pwm_t speed;
};
typedef struct mdv_pin mdv_pin;
enum mdv_error {
MDV_ERR_NO_ERROR = 0,
MDV_ERR_UNKNOWN_CONTROL_MODE = 1,
MDV_ERR_BAD_PARAM = 2,
MDV_ERR_ALREADY_RUNTIME_CALIBRATED = 3,
MDV_ERR_RESERVED_CONTROL_MODE = 4,
MDV_ERR_IN_CALIBRATION = 5,
MDV_ERR_MOTOR_NOT_MANUFACTURER_CALIBRATED = 6,
MDV_ERR_MOTOR_NOT_RUNTIME_CALIBRATED = 7,
MDV_ERR_NB_ERRORS = 8
};
typedef enum mdv_error mdv_error;
struct motordriver;
typedef struct motordriver motordriver;
// motordrivers reset output
extern gpio mdv_master_reset;
#define mdv_master_reset_enable() do{ gpio_clear(mdv_master_reset);}while(0)
#define mdv_master_reset_disable() do{ gpio_set(mdv_master_reset);} while(0)
void init_motordrivers(mdv_pin * mdv1_pin, mdv_pin * mdv2_pin);
void init_motordriver(motordriver * mdv,
mdv_pin * pin,
sbcp_reg_address address_start,
volatile unsigned int * pos_counter);
void mdv_reset_count(motordriver *m);
void mdv_process(motordriver * mdv);
int mdv_get_status_and_control_mode(motordriver *m);
#define mdv_get_status(m) (mdv_get_status_and_control_mode(m).status)
int mdv_get_control_mode(motordriver * mdv);
mdv_error mdv_set_control_mode(motordriver * mdv, int ctrl);
mdv_error mdv_runtime_calibrate(motordriver * mdv, int actual_position);
mdv_error mdv_set_smooth_interval(motordriver * mdv, int ts);
int mdv_get_smooth_interval(motordriver * mdv);
mdv_error mdv_set_goal_position(motordriver * mdv, int position);
mdv_error mdv_set_goal_speed(motordriver * mdv , int speed);
//mdv_error mdv_set_goal_torque(motordriver * mdv, mdv_torque_t torque);
int mdv_get_goal_position(motordriver * mdv);
int mdv_get_goal_speed(motordriver * mdv);
//mdv_torque_t mdv_get_goal_torque(motordriver * mdv);
int mdv_get_position(motordriver * mdv);
int mdv_get_speed(motordriver * mdv);
int mdv_get_targeted_torque(motordriver * mdv);
int mdv_get_current(motordriver * mdv);
int mdv_get_virtual_goal_position(motordriver *m);
int mdv_get_virtual_goal_speed(motordriver *m);
mdv_parameters * mdv_get_parameters(motordriver * m);
int mdv_get_maximal_acceleration(motordriver * m);
int mdv_get_maximal_speed(motordriver * m);
int mdv_get_maximal_torque(motordriver * m);
mdv_error mdv_set_maximal_acceleration(motordriver * m, int value);
mdv_error mdv_set_maximal_speed(motordriver * m, int value);
mdv_error mdv_set_maximal_torque(motordriver * m, int value);
mdv_error mdv_set_gear_ratio(motordriver * m, int gear_mult, int gear_div);
int mdv_get_gear_mult(motordriver * m);
int mdv_get_gear_div(motordriver * m);
int mdv_get_limit_position(motordriver * m);
void mdv_load_persistent_sbcp_settings();
typedef int ( * pos_evaluator_fptr )( );
mdv_error mdv_calibrate_motor(motordriver * mdv,
unsigned int limit_pos,
unsigned int min_calibration_region,
unsigned int max_calibration_region,
int calibration_offset,
pos_evaluator_fptr pos_evaluator,
unsigned int pos_evaluator_max_value,
uint8_t statically_calibrated);
/* never fails */
void mdv_uncalibrate_motor(motordriver * mdv);
extern motordriver * mdv1, * mdv2;
#endif /* MOTORDRIVER_H */

Event Timeline