H2M Teststand / Mbed 2 deprecated H2M_Snippets

Dependencies:   mbed

Fork of Low_Cost_PWM by Hans Dampf

Motorregler_simple.cpp

Committer:
Racer01014
Date:
2014-09-10
Revision:
5:a68e6c550e4b
Child:
6:f97371e6bc3e

File content as of revision 5:a68e6c550e4b:

#include "mbed.h"


PwmOut Motroregler_PWM(p22);

int void()
{

//***************************************************************************************************
//Control Motor rpm
 
int rpm_control(float motor_n_cmd, float motor_n_cur)
{
 
    static int motor_pwm_cmd_last = 900;
    //static float motor_n_last = 0;
 
    if (motor_n_cmd < 1.0) {
        Motroregler_PWM.pulsewidth_us(900);
        motor_pwm_cmd_last = 900;
//        motor_n_last = 0;
        return 1;
    }
 
    float motor_n_dif = motor_n_cmd - motor_n_cur;
 
    int motor_pwm_cmd = (int)(motor_pwm_cmd_last + motor_n_dif * 0.6 + 0.5); // round() ... works only for positive values
 
    pc.printf("cmd: %7.2f, cur: %7.2f, dif: %7.2f, motor_pwm_cmd: %4d, motor_pwm_dif: %4d, DMS: %f\n\r",
              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);
 
    if (motor_pwm_cmd > 1900) motor_pwm_cmd = 1900;
    else if (motor_pwm_cmd < 1010) motor_pwm_cmd = 1005;
 
    Motroregler_PWM.pulsewidth_us(motor_pwm_cmd);
    motor_pwm_cmd_last = motor_pwm_cmd;
 
    return 1;
 }
 
//***************************************************************************************************
//Control MOSFET pwm
}