Subversion Repositories Projects

Compare Revisions

Ignore whitespace Rev 332 → Rev 333

/FollowMe/main.c
12,9 → 12,12
#include "menu.h"
#include "printf_P.h"
#include "analog.h"
#include "ubx.h"
#include "gps.h"
#include "button.h"
 
 
#define FOLLOWME_INTERVAL 1000 // 1 second update
 
#ifdef USE_FOLLOWME
int16_t UBat = 120;
int16_t Zellenzahl = 0;
23,13 → 26,19
int16_t delay = 0;
#endif
 
#define GPS_RX_TIMEOUT 0x0001
uint16_t Error = 0;
 
typedef enum
{
STATE_UNDEFINED,
STATE_IDLE,
STATE_SEND_FOLLOWME
} SysState_t;
 
int main (void)
{
static uint16_t GPS_Timer = 0;
static uint16_t Error = 0;
static uint16_t FollowMe_Timer = 0;
static SysState_t SysState = STATE_UNDEFINED;
 
// disable interrupts global
cli();
51,10 → 60,10
// enable interrupts global
sei();
 
Fat16_Init();
 
LEDRED_OFF;
#ifdef USE_FOLLOWME
LEDGRN_ON;
#endif
 
// try to initialize the FAT 16 filesystem on the SD-Card
Fat16_Init();
70,23 → 79,80
printf("\r\n");
 
 
#ifdef USE_FOLLOWME
//BeepTime = 2000;
#endif
 
LCD_Clear();
 
GPS_Timer = SetDelay(1000);
FollowMe_Timer = SetDelay(FOLLOWME_INTERVAL);
 
while (1)
{
// check for button action
// get gps data to update the follow me position
GPS_Update();
 
// check for button action and change state resectively
if(GetButton())
{
BeepTime = 200;
Fat16_Init();
 
switch(SysState)
{
case STATE_IDLE:
if(!Error) SysState = STATE_SEND_FOLLOWME; // activate followme only of no error has occured
break;
 
case STATE_SEND_FOLLOWME:
SysState = STATE_IDLE;
break;
 
default:
SysState = STATE_IDLE;
break;
}
 
}
 
// state machine
switch(SysState)
{
case STATE_SEND_FOLLOWME:
if(CheckDelay(FollowMe_Timer)) // time for next message?
{
if(FollowMe.Position.Status == NEWDATA) // if new
{ // update remaining data
FollowMe_Timer = SetDelay(FOLLOWME_INTERVAL); // reset timer
FollowMe.Heading = -1; // invalid heading
FollowMe.ToleranceRadius = 1; // 1 meter
FollowMe.HoldTime = 60; // go home after 60s without any update
FollowMe.Event_Flag = 0; // no event
FollowMe.reserve[0] = 0; // reserve
FollowMe.reserve[1] = 0; // reserve
FollowMe.reserve[2] = 0; // reserve
FollowMe.reserve[3] = 0; // reserve
Request_SendFollowMe = 1; // triggers serial tranmission
 
}
else // now new position avalable (maybe bad gps signal condition)
{
FollowMe_Timer = SetDelay(FOLLOWME_INTERVAL/4); // reset timer on higer frequency
}
LEDGRN_TOGGLE; // indication of active follow me
}
break;
 
case STATE_IDLE:
// do nothing
LEDGRN_ON;
break;
 
default:
// triger to idle state
SysState = STATE_IDLE;
break;
 
}
 
 
// restart ADConversion if ready
if(ADReady)
{
107,7 → 173,7
// UAdc4 = R3/(R3+R2)*UBat= 3.9/(3.9+10)*UBat = UBat/3.564
UBat = (3 * UBat + (64 * Adc4) / 368) / 4;
DebugOut.Analog[8] = UBat;
 
// check for zellenzahl
if(PowerOn < 100)
{
117,9 → 183,9
}
DebugOut.Analog[16] = Zellenzahl;
DebugOut.Analog[17] = PowerOn;
 
//show recognised Zellenzahl to user
if(i < Zellenzahl && PowerOn >= 100 && BeepTime == 0 && delay > 1000)
if(i < Zellenzahl && PowerOn >= 100 && BeepTime == 0 && delay > 1000)
{
BeepTime = 100;
i++;
126,37 → 192,34
delay = 0;
}
if(delay < 1500) delay++;
 
// monitor battery undervoltage
if(UBat < Zellenzahl * 31 && PowerOn >= 100) BeepTime = 200;
if((UBat < Zellenzahl * 31) && (PowerOn >= 100))
{ // sound for low battery
BeepModulation = 0x0300;
if(!BeepTime)
{
BeepTime = 6000; // 0.6 seconds
}
Error |= ERROR_LOW_BAT;
}
else
{
Error &= ~ERROR_LOW_BAT;
}
#endif
ADReady = 0;
ADC_Enable(); // restart ad conversion sequence
}
 
// serial communication
USART0_ProcessRxData();
USART0_TransmitTxData();
 
if(GPSData.Status == NEWDATA)
{
Error &= ~GPS_RX_TIMEOUT; // clear possible error
GPS_Timer = SetDelay(1000); // reset timeout
if(CheckGPSOkay >= 5)
{
// trigger transmission of FollowMe message here.
// indicate error, blinking code tbd.
if(Error) LEDRED_ON;
else LEDRED_OFF;
 
CheckGPSOkay = 0;
}
GPSData.Status = PROCESSED;
}
else // invalid or already processed
{
if(CheckDelay(GPS_Timer))
{
Error |= GPS_RX_TIMEOUT;
}
}
 
USART0_ProcessRxData();
USART0_TransmitTxData();
}
return (1);
}