fixed battery detection
This commit is contained in:
		@ -19,7 +19,7 @@ charger::~charger() {
 | 
			
		||||
/* returns current charge-current in mA */
 | 
			
		||||
int charger::getCurrent() {
 | 
			
		||||
	mux.setChannel(charger_settings.chI);
 | 
			
		||||
	int tmp = (int) io.readAdc(0);
 | 
			
		||||
	int tmp = io.readAdc(0);
 | 
			
		||||
	int ret = (int) ((tmp * 1.4286) + 34.857); //TODO calibration and testing
 | 
			
		||||
	return ret;
 | 
			
		||||
}
 | 
			
		||||
@ -27,7 +27,7 @@ int charger::getCurrent() {
 | 
			
		||||
/* returns current battery voltage in V */
 | 
			
		||||
double charger::getVoltage() {
 | 
			
		||||
	mux.setChannel(charger_settings.chU);
 | 
			
		||||
	int tmp = (int) io.readAdc(0);
 | 
			
		||||
	int tmp = io.readAdc(0);
 | 
			
		||||
	if(tmp == 0) return 0;
 | 
			
		||||
	double ret = ((double) tmp) * 0.00615;
 | 
			
		||||
	return ret;
 | 
			
		||||
 | 
			
		||||
@ -92,3 +92,11 @@ ISR (TIMER1_COMPA_vect) {
 | 
			
		||||
	systemTime++; //increase system time
 | 
			
		||||
	updateChargers();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
#ifdef DEBUG
 | 
			
		||||
ISR(__vector_default) {
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -23,9 +23,9 @@ int main(void) {
 | 
			
		||||
	ui = (gui*) malloc(sizeof(gui));
 | 
			
		||||
 | 
			
		||||
	ui->gui_init();
 | 
			
		||||
 | 
			
		||||
#ifndef DEBUG
 | 
			
		||||
	_delay_ms(1000);
 | 
			
		||||
 | 
			
		||||
#endif
 | 
			
		||||
	ui->gui_info();
 | 
			
		||||
 | 
			
		||||
	io = (ioController*) malloc(sizeof(ioController));
 | 
			
		||||
@ -107,10 +107,12 @@ void updateChargers() {
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void checkForBattery() {
 | 
			
		||||
	bool activeChargers[CHARGER_SIZE];
 | 
			
		||||
void checkForBattery() {   		//TODO
 | 
			
		||||
	static int activeChargers[CHARGER_SIZE];
 | 
			
		||||
	static int errorCount[CHARGER_SIZE];
 | 
			
		||||
 | 
			
		||||
	for (int l = 0; l < CHARGER_SIZE; l++) {
 | 
			
		||||
 | 
			
		||||
		bool zero = false;
 | 
			
		||||
		double tmp1 = 0.0;
 | 
			
		||||
		double tmp2 = 0.0;
 | 
			
		||||
@ -120,7 +122,15 @@ void checkForBattery() {
 | 
			
		||||
			tmp2 = tmp1;
 | 
			
		||||
			tmp1 = chargers[l].getVoltage();
 | 
			
		||||
 | 
			
		||||
			if ((tmp1 == 0.0) || (((tmp1 - tmp2 > difference)) && i != 0)) {
 | 
			
		||||
#ifdef DEBUG
 | 
			
		||||
			//char x[50];
 | 
			
		||||
			// dtostrf(tmp1, 2, 2, x);
 | 
			
		||||
			//serialSend(x);
 | 
			
		||||
			//serialSend("\r\n");
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
			if ((tmp1 == 0.0)
 | 
			
		||||
					|| (((tmp1 - tmp2 > difference) || (tmp1 < 3.2)) && i != 0)) {
 | 
			
		||||
				zero = true;
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
@ -128,15 +138,31 @@ void checkForBattery() {
 | 
			
		||||
		if (!zero) {
 | 
			
		||||
			chargers[l].setStatus(true);
 | 
			
		||||
			//io->setActiveLED(true);
 | 
			
		||||
			if (!activeChargers[l]) {
 | 
			
		||||
			if (activeChargers[l] == 0) {
 | 
			
		||||
				chargers[l].setStartTime(clk->getTimeStamp());
 | 
			
		||||
 | 
			
		||||
#ifdef DEBUG
 | 
			
		||||
				serialSend("allowed\r\n");
 | 
			
		||||
#endif
 | 
			
		||||
			}
 | 
			
		||||
			activeChargers[l] = true;
 | 
			
		||||
			activeChargers[l] = 1;
 | 
			
		||||
			errorCount[l] = CONNECTION_TIMEOUT;
 | 
			
		||||
 | 
			
		||||
		} else {
 | 
			
		||||
			//io->setActiveLED(false);
 | 
			
		||||
			chargers[l].setStatus(false);
 | 
			
		||||
			chargers[l].reset();
 | 
			
		||||
			activeChargers[l] = false;
 | 
			
		||||
 | 
			
		||||
			errorCount[l]--;
 | 
			
		||||
#ifdef DEBUG
 | 
			
		||||
			serialSend("no connection\r\n");
 | 
			
		||||
#endif
 | 
			
		||||
			if (errorCount[l] == 0) {
 | 
			
		||||
				chargers[l].setStatus(false);
 | 
			
		||||
				activeChargers[l] = 0;
 | 
			
		||||
				chargers[l].reset(); //sets the capacity to zero
 | 
			
		||||
#ifdef DEBUG
 | 
			
		||||
				serialSend("blocked\r\n");
 | 
			
		||||
#endif
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
@ -193,7 +219,7 @@ void serialSetup(void) {
 | 
			
		||||
	UBRR0L = BUAD_RATE_CALC;
 | 
			
		||||
	//transimit and recieve enable
 | 
			
		||||
	UCSR0B = (1 << TXEN0) | (1 << TXCIE0) | (1 << RXEN0) | (1 << RXCIE0);
 | 
			
		||||
	UCSR0C = (1 << UCSZ01) | (1 << UCSZ00);//8 bit data format
 | 
			
		||||
	UCSR0C = (1 << UCSZ01) | (1 << UCSZ00);	//8 bit data format
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void serialSend(const char* sendString) {
 | 
			
		||||
 | 
			
		||||
@ -45,6 +45,7 @@ void serialSend(const char* sendString);
 | 
			
		||||
#endif
 | 
			
		||||
/* Charger Config */
 | 
			
		||||
#define CHARGER_SIZE 4
 | 
			
		||||
#define CONNECTION_TIMEOUT 3
 | 
			
		||||
 | 
			
		||||
// Charger 0
 | 
			
		||||
#define CH0_NR 0
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user