Beispiel #1
0
int main(void)
{
  uart0_init();

  setup_timers();

  setup_inputs();
  
  printf("EMU ECU\n");
 
  sei(); // Enable Global Interrupt

  uint16_t engine_stop_ms = ticks_ms();

  uint16_t loop_ms = 0;
  while (1)
  {
    uint16_t curr_rpm = rpm();
    if (!curr_rpm)
    {
      // keep the stop time sliding for when we attempt to start again
      if ((ticks_ms() - engine_stop_ms) > DWELL_TIME_MS)
      {
        engine_stop_ms = ticks_ms() - DWELL_TIME_MS;
      }
    }
    if (!ignition_enabled())
    {
      if (curr_rpm > 0 && (ticks_ms() - engine_stop_ms) > DWELL_TIME_MS)
      {
        ignition_enable();
        pump_enable();
        printf("engine start\n");
      }
    }
    else
    {
      if (curr_rpm > RPM_LIMIT)
      {
        printf("overrev - forced engine stop\n");
        ignition_disable();
        pump_disable();
        engine_stop_ms = ticks_ms();
      }
      if (!curr_rpm)
      {
        printf("engine has stopped\n");
        ignition_disable();
        pump_disable();
      }
    }

    uint16_t pwm_in = pwm_input();
    uint16_t pwm_out = clamp_pwm(pwm_in);
    set_pwm(0, pwm_out);

    const float t_scale = 1.0 / (float)(PWM_MAX - PWM_MIN);
    float throttle = (float)(pwm_out - PWM_MIN) * t_scale;

    update_inj_row(throttle);

    uint16_t ms = ticks_ms();
    //uint32_t us = ticks_us();

    if ((ms - loop_ms) >= 1000)
    {
      loop_ms += 1000;
      //start_adc();
      //int16_t a = analogue(0);

      //printf("pwm_in=%u pwm_out=%u us=%lu a0=%d\n", pwm_in, pwm_out, us, a);
      printf("pwm=%d throttle=%d rpm=%u ticks=%u \n", pwm_in, (int)(100*throttle), rpm(), inj_ticks_(rpm()));
    }
    //sleep(1000);
    //_delay_ms(1000);
  }  
}
Beispiel #2
0
//int tmp_val;//***********************************************************************************************************************************
void set_pwm(int chid, int val)
{
//	if(chid==0) tmp_val=tmp_val*-1;//***********************************************************************************************************************************
//	val=tmp_val;//***********************************************************************************************************************************
	
	pwm_cmd_buf[chid]=SIGN(val)*CLAMP(ABS(val),0,ec_cmd.command[chid].pwm_max); //Send back commanded value before gets inverted, deadband, etc
	
#if defined HB2_H2R1_J0J1 || defined HB2_H2R1_J2J3J4 
	//Safety limits
	/*int pwm_fwd=ec_cmd.command[chid].config&M3ACT_CONFIG_PWM_FWD_SIGN;
	//ec_debug[chid]=ma3_at_lower_bound(chid);
	if (ma3_at_lower_bound(chid))
		if ((val<0 && pwm_fwd) ||(val>0&&!pwm_fwd))
			val=0;
	if (ma3_at_upper_bound(chid))
		if ((val>0 && pwm_fwd) ||(val<0&&!pwm_fwd))
			val=0;*/
	int sign=SIGN(val);
	val=pwm_enc_limit(chid,val);
	val=pwm_deadband(chid, ABS(val));
	//Invert signal
	//pwm_cmd_buf[chid]=val;
	
	/*if (val<0)
		val-=ec_cmd.command[chid].pwm_db;
	if (val>0)
		val+= ec_cmd.command[chid].pwm_db;

	int pm=MIN(PWM_MAX_DUTY,ABS(ec_cmd.command[chid].pwm_max));*/
	pwm_duty_buf[chid]=invert_and_clamp_pwm(chid,val);
	//CLAMP(PWM_MAX_DUTY-ABS(val), PWM_MAX_DUTY-pm, PWM_MAX_DUTY);
	//pwm_duty_buf[chid]=CLAMP(ABS(val), PWM_MIN_DUTY,pm);
	pwm_cmd_buf[chid]=sign*CLAMP(val,0,ec_cmd.command[chid].pwm_max); 
#if defined HB2_H2R1_J0J1 
	if (chid==0)
	{
		if (sign<=0) SetPwmDir2; else ClrPwmDir2;
		//if (val==0) ClrEnableAmpA; else SetEnableAmpA;
		P1DC2 = pwm_duty_buf[chid];
	}
	if (chid==1)
	{
		if (sign<=0) SetPwmDir1; else ClrPwmDir1;
		//if (val==0) ClrEnableAmpB; else SetEnableAmpB;
		P1DC1 = pwm_duty_buf[chid];
	}
#endif
#if defined HB2_H2R1_J2J3J4 
	if (chid==0)
	{
		if (sign<=0) SetPwmDir1; else ClrPwmDir1;
		//if (val==0) ClrEnableAmpA; else SetEnableAmpA;
		P1DC1 = pwm_duty_buf[chid];
	}
	if (chid==1)
	{
		if (sign<=0) SetPwmDir2; else ClrPwmDir2;
		//if (val==0) ClrEnableAmpB; else SetEnableAmpB;
		P1DC2 = pwm_duty_buf[chid];
	}
	if (chid==2)
	{
		if (sign<=0) SetPwmDir3; else ClrPwmDir3;
		//if (val==0) ClrEnableAmpC; else SetEnableAmpC;
		P1DC3 = pwm_duty_buf[chid];
	}
#endif
#endif

#if defined HB2_H2R2_J0J1  || defined HB2_0_2_H2R3_J0J1
	//J0: L6235 Brushless Amp ; J1: L6205 Brushed Amplifier
	int sign=SIGN(val);
	val= pwm_enc_limit(chid, val);
	val=pwm_deadband(chid,ABS(val));
	if (chid==0)
	{
		//Invert signal
		pwm_duty_buf[chid]=clamp_pwm(chid,val);
		if (sign<=0) SetPwmDirJ0; else ClrPwmDirJ0;
		P1DC2 = pwm_duty_buf[chid];
	}
	if (chid==1)
	{
		if (sign<0) //Invert polarity for this direction
			pwm_duty_buf[chid]=invert_and_clamp_pwm(chid,val);
		else
			pwm_duty_buf[chid]=clamp_pwm(chid,val);
			if (sign<=0) SetPwmDirJ1; else ClrPwmDirJ1;
			P1DC1 = pwm_duty_buf[chid];
	}
	pwm_cmd_buf[chid]=sign*CLAMP(val,0,ec_cmd.command[chid].pwm_max);
#endif

#if defined HB2_H2R2_J2J3J4  || defined HB2_0_2_H2R3_J2J3J4
	//3CH L6205 Amplifier
	int sign=SIGN(val);
	val= pwm_enc_limit(chid, val);
	val=pwm_deadband(chid,ABS(val));
	if (sign<0) //Invert polarity for this direction
		pwm_duty_buf[chid]=invert_and_clamp_pwm(chid,val);
	else
		pwm_duty_buf[chid]=clamp_pwm(chid,val);
	if (chid==0)
	{
		if (sign<=0) SetPwmDirJ2; else ClrPwmDirJ2;
		P1DC1 = pwm_duty_buf[chid];
	}
	if (chid==1)
	{
		if (sign<=0) SetPwmDirJ3; else ClrPwmDirJ3;
		P1DC2 = pwm_duty_buf[chid];
	}
	if (chid==2)
	{
		if (sign<=0) SetPwmDirJ4; else ClrPwmDirJ4;
		P1DC3 = pwm_duty_buf[chid];
	}
	pwm_cmd_buf[chid]=sign*CLAMP(val,0,ec_cmd.command[chid].pwm_max);
#endif
}