• Main Page
  • Modules
  • Data Structures
  • Files
  • File List
  • Globals

ula_repl.c

Go to the documentation of this file.
00001 /*@{*//*@}*/
00016 
00017 /******************************************************************************/
00018 /* Includes (#include)                                                        */
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 /* Own header files */
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 /* Constants (#define)                *//*@{*/
00051 /* End: Constants (#define)                                             *//*@}*/
00052 /******************************************************************************/
00053 
00054 /******************************************************************************/
00055 /* Macro definitions (#define)           *//*@{*/
00056 /* End: Macro definitions (#define)                                     *//*@}*/
00057 /******************************************************************************/
00058 
00059 /******************************************************************************/
00060 /* Basic types (typedef)                 *//*@{*/
00061 /* End: Basic types (typedef)                                           *//*@}*/
00062 /******************************************************************************/
00063 
00064 /******************************************************************************/
00065 /* Local constants (const)      *//*@{*/
00066 /* End: Local constants (const)                                         *//*@}*/
00067 /******************************************************************************/
00068 
00069 /******************************************************************************/
00070 /* Local variables              *//*@{*/
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 /* End: Local variables                                                 *//*@}*/
00091 /******************************************************************************/
00092 
00093 /******************************************************************************/
00094 /* Global variables           *//*@{*/
00095 /* End: Global variables                                                *//*@}*/
00096 /******************************************************************************/
00097 
00098 /******************************************************************************/
00099 /* Prototype declarations                                                     */
00100        int    main            (void);
00101 STATIC E_BOOL mg_RelayControl (void);
00102 /* End: Prototype declarations                                                */
00103 /******************************************************************************/
00104 
00105 /******************************************************************************/
00106 /* Local functions             *//*@{*/
00107 
00108 
00109 /* ****************************************************************************/
00140 /* ****************************************************************************/
00141 int main (void)
00142 {
00143   UINT8   u8Help = 0U;
00144   
00145   E_BOOL  bMotorEnabled = FALSE;
00146   
00147   /* init IO ports */
00148   init_InitIO();
00149   
00150   /* init modules */
00151   cli();
00152   init_InitModules();
00153   sei(); 
00154   
00155   ALL_LEDS_OFF();
00156   
00157   /* get settings from EEPROM memory */
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   /* print firmware version on serial out */
00166   printf("ULA replacement V%d\n\n", pgm_read_word(MEMPOS_VER));
00167   printf("CRC: 0x%08lX\n\n",        safety_GetCRC());
00168   
00169   /* print settings on serial out */
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   /* put relay and speaker to default conditions */
00179   relay_Enable(FALSE);
00180   speaker_Off();
00181   
00182   /* reset watchdog before entering the main loop */
00183   wdt_reset();
00184   
00185   // zum testen: while(1) { asm("nop"); }
00186   
00187   /* application loop */
00188   while (1)
00189   {
00190     /* lock the timer bits during this while loop cycle */
00191     timer_LockTicks();
00192     
00193     /* meassure momentary voltage and current values */
00194     voltage_Update();
00195     current_Update();
00196     
00197     /* meassure the motor temperature */
00198     temp_Update();
00199     
00200     /* every 1024ms: check temperature for overheating */
00201     if(TIMER_IS_TICK1024())
00202     {
00203       temp_Check();
00204     }
00205     
00206     /* activate the fan if the average temperature exeeds or equals the
00207        configurated temperature threshold */
00208     if(temp_GetCelsius(temp_GetAvg()) >= mg_u16FanTemp)
00209     {
00210       fan_Set(ON);
00211     }
00212     
00213     /* check every 10240ms if we can deactivate the fan */
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           /* fan may be deactivated only if there is no temperature 
00223              error condition set */
00224           if((safety_GetCondition() & COND_TEMP_ERR) == 0U)
00225           {
00226             fan_Set(OFF);
00227           }
00228         }
00229       }
00230     }
00231     
00232     /* every 512ms: calculate average voltage and current draw */
00233     if(TIMER_IS_TICK512())
00234     {
00235       voltage_UpdateAvg();
00236       current_UpdateSum();
00237     }
00238     
00239     /* every 256ms: update average motor current */
00240     if(TIMER_IS_TICK128())
00241     {
00242       current_UpdateAvg();
00243     }
00244     
00245     /* always check for speed and pedal signal timeouts */
00246     speed_CheckTimeout(timer_GetSystemTime());
00247     pedal_GetTimeout();
00248     
00249     /* after all checks and data updates above, we are ready to determine 
00250        wether the relay has to be enabled or disabled */
00251     bMotorEnabled = mg_RelayControl();
00252     
00253     /* Memory test.
00254        The memory range to be tested is byte address 0 to 7164 (3582 words).
00255        Note: This function will not return in case of a memory failure. */
00256     safety_MemoryTest();
00257     
00258     /* every 256ms: handle further jobs */
00259     if(TIMER_IS_TICK256())
00260     {
00261       /* reset watchdog */
00262       wdt_reset();
00263       
00264       /* print information on serial out */
00265       printf("#");
00266       printf("%lu|", speed_GetDistance());    // distance
00267       printf("%lu|", SPEED_GET());            // speed (10th of km/h)
00268       printf("%lu|", pedal_GetRpm());         // upm
00269       printf("%lu|", pedal_GetTimeout());     // timeout
00270       printf("%u|",  adc_Read(ADC_SWITCH));   // switch
00271       printf("%u|",  temp_GetAvg());          // vth
00272       printf("%u|",  voltage_GetValue());     // voltage
00273       printf("%u|",  voltage_GetRef());       // vref
00274       printf("%u|",  current_GetValue());     // current
00275       printf("%u|",  bMotorEnabled);          // motor enabled?
00276       printf("%u|",  safety_GetCondition());  // condition
00277       printf("%lu|", timer_GetSystemTime());  // timestamp
00278       printf("\n");
00279     }
00280     
00281   } /* end of: while(1) application loop */
00282   
00283   return (1); /* never reached */
00284   
00285 } /* End: main() */
00286 
00287 
00288 /* ****************************************************************************/
00300 /* ****************************************************************************/
00301 E_BOOL mg_RelayControl (void)
00302 {
00303   E_BOOL bRet = TRUE;
00304   
00305   /* motor will be disabled if any error condition occours */
00306   if(safety_GetCondition() >= COND_VOLT_ERR)
00307   {
00308     bRet = FALSE;
00309   }
00310   /* motor will be disabled if momentary motor load is too high */
00311   else if(current_GetValue() >= MAX_CURRENT)
00312   {
00313     bRet = FALSE;
00314   }
00315   /* motor will be disabled if pedelec settings require it */
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   /* enable/disable motor relay */
00339   if(FALSE == bRet)
00340   {
00341     relay_Enable(FALSE);
00342   }
00343   else
00344   {
00345     relay_Enable(TRUE);
00346   }
00347   
00348   return (bRet);
00349 } /* End: mg_RelayControl() */
00350 
00351 
00352 
00353 /* End: Local functions                                                 *//*@}*/
00354 /******************************************************************************/
00355 
00356 /******************************************************************************/
00357 /* Global functions          *//*@{*/
00358 /* End: Global functions                                                *//*@}*/
00359 /******************************************************************************/
00360 
00361 /**** Last line of code                                                    ****/

Generated on Sun Jan 23 2011 14:05:40 for ULA replacement firmware by  doxygen 1.7.2