H2M Teststand / Mbed 2 deprecated H2M_Snippets

Dependencies:   mbed

Fork of Low_Cost_PWM by Hans Dampf

Committer:
Racer01014
Date:
Wed Sep 10 12:51:11 2014 +0000
Revision:
5:a68e6c550e4b
Child:
6:f97371e6bc3e
Auf ein neues;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Racer01014 5:a68e6c550e4b 1 #include "mbed.h"
Racer01014 5:a68e6c550e4b 2
Racer01014 5:a68e6c550e4b 3
Racer01014 5:a68e6c550e4b 4 PwmOut Motroregler_PWM(p22);
Racer01014 5:a68e6c550e4b 5
Racer01014 5:a68e6c550e4b 6 int void()
Racer01014 5:a68e6c550e4b 7 {
Racer01014 5:a68e6c550e4b 8
Racer01014 5:a68e6c550e4b 9 //***************************************************************************************************
Racer01014 5:a68e6c550e4b 10 //Control Motor rpm
Racer01014 5:a68e6c550e4b 11
Racer01014 5:a68e6c550e4b 12 int rpm_control(float motor_n_cmd, float motor_n_cur)
Racer01014 5:a68e6c550e4b 13 {
Racer01014 5:a68e6c550e4b 14
Racer01014 5:a68e6c550e4b 15 static int motor_pwm_cmd_last = 900;
Racer01014 5:a68e6c550e4b 16 //static float motor_n_last = 0;
Racer01014 5:a68e6c550e4b 17
Racer01014 5:a68e6c550e4b 18 if (motor_n_cmd < 1.0) {
Racer01014 5:a68e6c550e4b 19 Motroregler_PWM.pulsewidth_us(900);
Racer01014 5:a68e6c550e4b 20 motor_pwm_cmd_last = 900;
Racer01014 5:a68e6c550e4b 21 // motor_n_last = 0;
Racer01014 5:a68e6c550e4b 22 return 1;
Racer01014 5:a68e6c550e4b 23 }
Racer01014 5:a68e6c550e4b 24
Racer01014 5:a68e6c550e4b 25 float motor_n_dif = motor_n_cmd - motor_n_cur;
Racer01014 5:a68e6c550e4b 26
Racer01014 5:a68e6c550e4b 27 int motor_pwm_cmd = (int)(motor_pwm_cmd_last + motor_n_dif * 0.6 + 0.5); // round() ... works only for positive values
Racer01014 5:a68e6c550e4b 28
Racer01014 5:a68e6c550e4b 29 pc.printf("cmd: %7.2f, cur: %7.2f, dif: %7.2f, motor_pwm_cmd: %4d, motor_pwm_dif: %4d, DMS: %f\n\r",
Racer01014 5:a68e6c550e4b 30 motor_n_cmd*60, motor_n_cur*60, motor_n_dif*60, motor_pwm_cmd, motor_pwm_cmd-motor_pwm_cmd_last, ((int)DMS_0.read_u16())/65536.0*3.3);
Racer01014 5:a68e6c550e4b 31
Racer01014 5:a68e6c550e4b 32 if (motor_pwm_cmd > 1900) motor_pwm_cmd = 1900;
Racer01014 5:a68e6c550e4b 33 else if (motor_pwm_cmd < 1010) motor_pwm_cmd = 1005;
Racer01014 5:a68e6c550e4b 34
Racer01014 5:a68e6c550e4b 35 Motroregler_PWM.pulsewidth_us(motor_pwm_cmd);
Racer01014 5:a68e6c550e4b 36 motor_pwm_cmd_last = motor_pwm_cmd;
Racer01014 5:a68e6c550e4b 37
Racer01014 5:a68e6c550e4b 38 return 1;
Racer01014 5:a68e6c550e4b 39 }
Racer01014 5:a68e6c550e4b 40
Racer01014 5:a68e6c550e4b 41 //***************************************************************************************************
Racer01014 5:a68e6c550e4b 42 //Control MOSFET pwm
Racer01014 5:a68e6c550e4b 43 }