PRBS signal on Haptic_hid with output signal of position

Dependencies:   MODSERIAL USBDevice compensation_tables mbed-dsp mbed

Fork of haptic_hid by First Last

Committer:
vsluiter
Date:
Mon Jun 19 08:24:44 2017 +0000
Revision:
4:9d37f163d64c
Parent:
3:10863117020c
Changed from cout to printf, changed timing a bit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
tomlankhorst 0:f3cf9865b7be 1 #ifndef MAIN_H
tomlankhorst 0:f3cf9865b7be 2 #define MAIN_H
tomlankhorst 0:f3cf9865b7be 3
tomlankhorst 0:f3cf9865b7be 4 // System
tomlankhorst 0:f3cf9865b7be 5 #define ENABLE_COGGING_COMPENSATION 1
tomlankhorst 0:f3cf9865b7be 6 #define ENABLE_POSITION_COMPENSATION 1
tomlankhorst 0:f3cf9865b7be 7 #define ENABLE_FRICTION_COMPENSATION 1
tomlankhorst 0:f3cf9865b7be 8 // Torque Controller
tomlankhorst 0:f3cf9865b7be 9 #define TORQUE_CONTROLLER_INTERVAL_US 100
tomlankhorst 0:f3cf9865b7be 10 #define TORQUE_CONTROLLER_INTERVAL_INV 10000
tomlankhorst 0:f3cf9865b7be 11 #define TORQUE_LIMIT 2.0f
tomlankhorst 0:f3cf9865b7be 12 #define POSITION_RESOLUTION 8192
tomlankhorst 0:f3cf9865b7be 13 #define POSITION_ANTIALIAS 4096
tomlankhorst 0:f3cf9865b7be 14 #define ELECTRICAL_POSITION_TO_RAD 0.038349f // 2*pi/(8192/50)
tomlankhorst 0:f3cf9865b7be 15 // Friction Model
tomlankhorst 0:f3cf9865b7be 16 #define COULOMB_VELOCITY_CONST 9.0f
tomlankhorst 0:f3cf9865b7be 17 #define STRIBECK_VELOCITY_CONST 0.5f
tomlankhorst 0:f3cf9865b7be 18 #define COULOMB_FRICTION 0.058f
tomlankhorst 0:f3cf9865b7be 19 #define STRIBECK_FRICTION 0.060f
tomlankhorst 0:f3cf9865b7be 20 #define VISCOUS_FRICTION_COEF_FWD (float)2.65e-3 //2.16561e-3
tomlankhorst 0:f3cf9865b7be 21 #define VISCOUS_FRICTION_COEF_REV (float)2.55e-3 //2.16561e-3
tomlankhorst 0:f3cf9865b7be 22 #define ENABLE_DITHER 0
tomlankhorst 0:f3cf9865b7be 23 #define DITHER_FORCE 0.02f
tomlankhorst 0:f3cf9865b7be 24 #define DITHER_TICKS 70
tomlankhorst 0:f3cf9865b7be 25 // Current Controller
tomlankhorst 0:f3cf9865b7be 26 #define CURRENT_CONTROLLER_KP 1.0f
tomlankhorst 0:f3cf9865b7be 27 #define CURRENT_CONTROLLER_KI 7500.0f
tomlankhorst 0:f3cf9865b7be 28 #define CURRENT_CONTROLLER_I_LIMIT 0.0001f
tomlankhorst 0:f3cf9865b7be 29 // Function-likes
tomlankhorst 0:f3cf9865b7be 30 #define GET_POSITION() ((int)(spi.write(0xFFFF) & 0x1FFF))
tomlankhorst 0:f3cf9865b7be 31 #define GET_CURRENT_A() (-6.6f*(current_sensor_a.read() - current_sensor_a_offset))
tomlankhorst 0:f3cf9865b7be 32 #define GET_CURRENT_B() (-6.6f*(current_sensor_b.read() - current_sensor_b_offset))
tomlankhorst 0:f3cf9865b7be 33 #define LOWPASSIIR(v,i,w) v+=w*(i-v)
tomlankhorst 0:f3cf9865b7be 34 #define SGN(x) (x > 0 ? 1 : (x < 0 ? -1 : 0))
tomlankhorst 0:f3cf9865b7be 35 #define ABSPOS() (position - position_ref + 8192*position_offset_count) // dont forget POSITION_RESOLUTION
tomlankhorst 0:f3cf9865b7be 36 // Speed estimator
tomlankhorst 0:f3cf9865b7be 37 #define SPEED_ESTIMATOR_FAST_GAIN 50
tomlankhorst 0:f3cf9865b7be 38 #define SPEED_ESTIMATOR_DEADBAND 1
tomlankhorst 0:f3cf9865b7be 39 #define SPEED_ESTIMATOR_FILTER 0.02f
tomlankhorst 0:f3cf9865b7be 40 #define SAMPLE_TIME_US 100
tomlankhorst 0:f3cf9865b7be 41 #define SAMPLE_TIME_INVERSE_S 10000.0f
tomlankhorst 0:f3cf9865b7be 42
tomlankhorst 0:f3cf9865b7be 43 // Current Controller Variables
tomlankhorst 0:f3cf9865b7be 44 float current_sensor_a_offset = 0;
tomlankhorst 0:f3cf9865b7be 45 float current_sensor_b_offset = 0;
tomlankhorst 0:f3cf9865b7be 46 // Torque Controller Variables
tomlankhorst 0:f3cf9865b7be 47 float torque = 0;
tomlankhorst 0:f3cf9865b7be 48 float speed;
tomlankhorst 0:f3cf9865b7be 49 float acceleration;
tomlankhorst 0:f3cf9865b7be 50 int32_t position;
tomlankhorst 0:f3cf9865b7be 51 int position_offset_count = 0;
tomlankhorst 0:f3cf9865b7be 52 int position_ref;
tomlankhorst 0:f3cf9865b7be 53 int dither_tick = 0;
tomlankhorst 0:f3cf9865b7be 54 // Impedance controller variables
tomlankhorst 0:f3cf9865b7be 55 float ZControl_K = 0;
tomlankhorst 0:f3cf9865b7be 56 float ZControl_B = 0;
tomlankhorst 0:f3cf9865b7be 57 float ZControl_I = 0;
tomlankhorst 0:f3cf9865b7be 58 int ZControl_RefPos = 0;
tomlankhorst 0:f3cf9865b7be 59
tomlankhorst 0:f3cf9865b7be 60 // IO Variables
vsluiter 1:24b7ab90081a 61 //USBHID hid(16,16);
vsluiter 1:24b7ab90081a 62 //HID_REPORT send_report;
vsluiter 1:24b7ab90081a 63 //HID_REPORT recv_report;
tomlankhorst 0:f3cf9865b7be 64 DigitalIn user_btn(p23);
tomlankhorst 0:f3cf9865b7be 65 SPI spi(NC, p6, p7);
vsluiter 4:9d37f163d64c 66 MODSERIAL pc(USBTX, USBRX);
tomlankhorst 0:f3cf9865b7be 67 AnalogIn current_sensor_a(p15);
tomlankhorst 0:f3cf9865b7be 68 AnalogIn current_sensor_b(p16);
tomlankhorst 0:f3cf9865b7be 69 PwmOut driver_1a(p28);
tomlankhorst 0:f3cf9865b7be 70 PwmOut driver_2a(p27);
tomlankhorst 0:f3cf9865b7be 71 PwmOut driver_1b(p26);
tomlankhorst 0:f3cf9865b7be 72 PwmOut driver_2b(p25);
tomlankhorst 0:f3cf9865b7be 73 DigitalOut driver_enable_a(p34);
tomlankhorst 0:f3cf9865b7be 74 DigitalOut driver_enable_b(p33);
tomlankhorst 0:f3cf9865b7be 75 PwmOut driver_vref_ab(p29);
tomlankhorst 0:f3cf9865b7be 76 DigitalOut info_led_1(LED1);
tomlankhorst 0:f3cf9865b7be 77 DigitalOut info_led_2(LED2);
tomlankhorst 0:f3cf9865b7be 78 DigitalOut info_led_3(LED3);
tomlankhorst 0:f3cf9865b7be 79 DigitalOut info_led_4(LED4);
tomlankhorst 0:f3cf9865b7be 80 // Ticker Variables
tomlankhorst 0:f3cf9865b7be 81 Ticker torque_controller;
tomlankhorst 0:f3cf9865b7be 82 Ticker hid_controller;
tomlankhorst 0:f3cf9865b7be 83
tomlankhorst 0:f3cf9865b7be 84 // Function declarations
tomlankhorst 0:f3cf9865b7be 85 void calibrate_current_sensor();
tomlankhorst 0:f3cf9865b7be 86 void calibrate_position();
tomlankhorst 0:f3cf9865b7be 87 void initialize_io();
tomlankhorst 0:f3cf9865b7be 88 void torque_control();
JKaal 3:10863117020c 89 void initialiseer_prbs();
vsluiter 1:24b7ab90081a 90 //void hid_control();
tomlankhorst 0:f3cf9865b7be 91
tomlankhorst 0:f3cf9865b7be 92 #endif