Go to the documentation of this file.00001
00016
00017
00018
00019 #include <avr/wdt.h>
00020 #include <util/delay.h>
00021 #include <avr/interrupt.h>
00022 #include <avr/pgmspace.h>
00023 #include <stdio.h>
00024
00025
00026 #include "xtypes.h"
00027 #include "global.h"
00028
00029 #include "adc.h"
00030 #include "fan.h"
00031 #include "init.h"
00032 #include "temp.h"
00033 #include "timer.h"
00034 #include "pedal.h"
00035 #include "speed.h"
00036 #include "relay.h"
00037 #include "safety.h"
00038 #include "memory.h"
00039 #include "speaker.h"
00040 #include "voltage.h"
00041 #include "current.h"
00042
00043
00044
00045 #define EXPORT
00046 #include "ula_repl.h"
00047 #undef EXPORT
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00073 STATIC E_BOOL mg_bIsPedelec = TRUE;
00074
00076 STATIC UINT16 mg_u16SpeedLimitMax = SPEEDLIMIT_MAX;
00077
00079 STATIC UINT16 mg_u16SpeedLimitMin = SPEEDLIMIT_MIN;
00080
00082 STATIC UINT16 mg_u16FanTemp = VALUE_TEMP_FAN;
00083
00085 STATIC UINT8 mg_u8NumMagnetsTacho = DEFAULT_MAGNET_NUMBER_TACHO;
00086
00088 STATIC UINT8 mg_u8NumMagnetsPedal = DEFAULT_MAGNET_NUMBER_PEDAL;
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100 int main (void);
00101 STATIC E_BOOL mg_RelayControl (void);
00102
00103
00104
00105
00106
00107
00108
00109
00140
00141 int main (void)
00142 {
00143 UINT8 u8Help = 0U;
00144
00145 E_BOOL bMotorEnabled = FALSE;
00146
00147
00148 init_InitIO();
00149
00150
00151 cli();
00152 init_InitModules();
00153 sei();
00154
00155 ALL_LEDS_OFF();
00156
00157
00158 mg_bIsPedelec = memory_Get()->bIsPedelec;
00159 mg_u16SpeedLimitMax = memory_Get()->u16SpeedLimitMax;
00160 mg_u16SpeedLimitMin = memory_Get()->u16SpeedLimitMin;
00161 mg_u16FanTemp = memory_Get()->u16TempFan;
00162 mg_u8NumMagnetsTacho = memory_Get()->u8NumMagnetsTacho;
00163 mg_u8NumMagnetsPedal = memory_Get()->u8NumMagnetsPedal;
00164
00165
00166 printf("ULA replacement V%d\n\n", pgm_read_word(MEMPOS_VER));
00167 printf("CRC: 0x%08lX\n\n", safety_GetCRC());
00168
00169
00170 printf("PM: %d\n", mg_bIsPedelec);
00171 printf("WS: %umm\n", memory_Get()->u16WheelSize);
00172 printf("LMX: %u\n", mg_u16SpeedLimitMax);
00173 printf("LMN: %u\n", mg_u16SpeedLimitMin);
00174 printf("FT: %u\n", mg_u16FanTemp);
00175 printf("NMT: %u\n", mg_u8NumMagnetsTacho);
00176 printf("NMP: %u\n\n", mg_u8NumMagnetsPedal);
00177
00178
00179 relay_Enable(FALSE);
00180 speaker_Off();
00181
00182
00183 wdt_reset();
00184
00185
00186
00187
00188 while (1)
00189 {
00190
00191 timer_LockTicks();
00192
00193
00194 voltage_Update();
00195 current_Update();
00196
00197
00198 temp_Update();
00199
00200
00201 if(TIMER_IS_TICK1024())
00202 {
00203 temp_Check();
00204 }
00205
00206
00207
00208 if(temp_GetCelsius(temp_GetAvg()) >= mg_u16FanTemp)
00209 {
00210 fan_Set(ON);
00211 }
00212
00213
00214 if(TIMER_IS_TICK1024())
00215 {
00216 u8Help++;
00217 if(u8Help >= 10U)
00218 {
00219 u8Help = 0U;
00220 if(temp_GetCelsius(temp_GetAvg()) < mg_u16FanTemp)
00221 {
00222
00223
00224 if((safety_GetCondition() & COND_TEMP_ERR) == 0U)
00225 {
00226 fan_Set(OFF);
00227 }
00228 }
00229 }
00230 }
00231
00232
00233 if(TIMER_IS_TICK512())
00234 {
00235 voltage_UpdateAvg();
00236 current_UpdateSum();
00237 }
00238
00239
00240 if(TIMER_IS_TICK128())
00241 {
00242 current_UpdateAvg();
00243 }
00244
00245
00246 speed_CheckTimeout(timer_GetSystemTime());
00247 pedal_GetTimeout();
00248
00249
00250
00251 bMotorEnabled = mg_RelayControl();
00252
00253
00254
00255
00256 safety_MemoryTest();
00257
00258
00259 if(TIMER_IS_TICK256())
00260 {
00261
00262 wdt_reset();
00263
00264
00265 printf("#");
00266 printf("%lu|", speed_GetDistance());
00267 printf("%lu|", SPEED_GET());
00268 printf("%lu|", pedal_GetRpm());
00269 printf("%lu|", pedal_GetTimeout());
00270 printf("%u|", adc_Read(ADC_SWITCH));
00271 printf("%u|", temp_GetAvg());
00272 printf("%u|", voltage_GetValue());
00273 printf("%u|", voltage_GetRef());
00274 printf("%u|", current_GetValue());
00275 printf("%u|", bMotorEnabled);
00276 printf("%u|", safety_GetCondition());
00277 printf("%lu|", timer_GetSystemTime());
00278 printf("\n");
00279 }
00280
00281 }
00282
00283 return (1);
00284
00285 }
00286
00287
00288
00300
00301 E_BOOL mg_RelayControl (void)
00302 {
00303 E_BOOL bRet = TRUE;
00304
00305
00306 if(safety_GetCondition() >= COND_VOLT_ERR)
00307 {
00308 bRet = FALSE;
00309 }
00310
00311 else if(current_GetValue() >= MAX_CURRENT)
00312 {
00313 bRet = FALSE;
00314 }
00315
00316 else if (FALSE != mg_bIsPedelec)
00317 {
00318 if (SPEED_GET() < mg_u16SpeedLimitMin)
00319 {
00320 bRet = FALSE;
00321 }
00322 else
00323 {
00324 if(SPEED_GET() > mg_u16SpeedLimitMax)
00325 {
00326 bRet = FALSE;
00327 }
00328 else
00329 {
00330 if (pedal_GetTimeout() >= PEDAL_TIMEOUT)
00331 {
00332 bRet = FALSE;
00333 }
00334 }
00335 }
00336 }
00337
00338
00339 if(FALSE == bRet)
00340 {
00341 relay_Enable(FALSE);
00342 }
00343 else
00344 {
00345 relay_Enable(TRUE);
00346 }
00347
00348 return (bRet);
00349 }
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361