Example #1
0
void consumptionrate_sample() {
	// take energymeter sample
	static energymeter_sample now;
	energymeter_sampling(&now);

	// calculate energy consumption rate and save in buffer
	#if CONSUMPTIONRATE_SOLARENERGY_BATTERYPREDICTION
		fpint fp_drain = energymeter_calculate_drain(&last_energymeter_sample, &now, ENERGYMETER_DRAIN_WITH_INCOMPLETE_TIMEFRAMES);
		fpint fp_battery = fpint_sub(battery_capacity(), fp_last_battery_capacity);
		fpint fp_consumptionrate = fpint_max(fpint_to(0), fpint_add(fp_battery, fp_drain));
		debug("[CONSUMPTIONRATE] drain=%smAh, batterydiff=%smAh, consumptionrate=%smAh\n", debug_fpint(fp_drain), debug_fpint(fp_battery), debug_fpint(fp_consumptionrate));
	#else
		fpint fp_consumptionrate = fp_solar_energy;
		fp_solar_energy = 0;
		debug("[CONSUMPTIONRATE] consumptionrate=%smAh\n", debug_fpint(fp_consumptionrate));
	#endif
	circularbuffer_save(consumptionrate_samples, CONSUMPTIONRATE_SAMPLES, fp_consumptionrate, &nextpos, &consumptionrate_saved);

	#if DEBUG & DEBUG_ON
		debug("[CONSUMPTIONRATE] Samples=[");
		int i = 0;
		while(i < consumptionrate_saved) {
			debug("%s", debug_fpint(consumptionrate_samples[i]));
			if(++i < consumptionrate_saved)
				debug(", ");
		}
		debug("]\n");
	#endif

	// save actual samples
	memcpy(&last_energymeter_sample, &now, sizeof(energymeter_sample));
	fp_last_battery_capacity = battery_capacity();
}
Example #2
0
void consumptionrate_init() {
    energymeter_sampling(&last_energymeter_sample);
    fp_last_battery_capacity = battery_capacity();
	#if CONSUMPTIONRATE_SOLARENERGY_BATTERYPREDICTION == 0
		process_start(&consumptionrate_solarpanel_process, NULL);
	#endif

}
Example #3
0
/*
 * check battery capacity
 */
int check_battery(void)
{
	int bat_threshold;

	bat_threshold = atoi(tboot_config_get(BAT_THRESHOLD_KEY));
	if (bat_threshold < 0 || bat_threshold > 100)
		pr_warning("you set battery threshold to %d, is that right?\n", bat_threshold);

	if (battery_capacity() < bat_threshold) {
		pr_debug("battery is too low, lower than %d\n", bat_threshold);
		return -1;
	}
	return 0;
}
Example #4
0
/**
 * calculates the new sampling rate (the "magic" part of SDF is done here)
 */
static void update_sampling_rate(struct etimer* sample_timer, struct etimer* transmit_timer, int real_calculation) {
	// samplingrate energy sample
	if(last_samplingrate_energysample == 0 || (time() - last_samplingrate_energysample) / SDF_SAMPLINGRATE_UPDATEINTERVAL > 0) {
		// take sample
		// first call with no information on last samplingrate and childs will
		// not do anything: reference energy sample is accquired
		samplingrate_sample_energy_drain(samplingrate, last_samplingrate_childs);

		// set infos for next sample
		last_samplingrate_childs = udphelper_childs_all_count();
		last_samplingrate_energysample = time();
	}

	// calculate samplingrate
	if(real_calculation) {
		// calculate samplingrate
		if((time() - time_init) / SDF_INITIALIZATIONPHASE == 0) {
			fpint fp_samplingrate = fpint_div(fpint_to(SDF_SAMPLINGRATE_MINIMAL), fpint_to(SPEEDMULTIPLIER));
			samplingrate = fpint_from(fpint_round(fp_samplingrate));
		} else {
			int max_samples = (udphelper_address_equals(udphelper_address_parent(&ip_parent), &ip_sink)) ? -1 : last_parent_samlingrate;
			samplingrate = samplingrate_calculate(max_samples);

			// send sampling rate to all childs
			if(udphelper_childs_direct_count() > 0) {
				samplingrate_children_transmitted = 0;
				samplingrate_children_count = udphelper_childs_direct_count();
				etimer_restart(transmit_timer);
			}
		}

		// debug
		printf("[%lds] ", time());
		printf("%d samples (battery=%ldmAh)\n", samplingrate, fpint_from(battery_capacity()));
	}

	// set samplingrate timer (evenly distributed samples with gaps at start and end)
	int sampling_delay = (SDF_SAMPLINGRATE_UPDATEINTERVAL - 60) / samplingrate;
	etimer_set(sample_timer, CLOCK_SECOND * sampling_delay / SPEEDMULTIPLIER);
	etimer_restart(sample_timer);

	// reset old samples counter (new interval begins)
	sampled = 0;
}
Example #5
0
/*
 * call back function to update tboot UI
 */
static void ui_callback(void *arg)
{
	char buf_usb[32];
	char buf_charger[32];
	char buf_bat[32];
	int status;
	static saved_usb_status = STATUS_UNKOWN;
	static saved_charger_status = STATUS_UNKOWN;

	status = usb_status();
	if (status == STATUS_UNKOWN)
		snprintf(buf_usb, sizeof(buf_usb), "USB: Unknow");
	else
		snprintf(buf_usb, sizeof(buf_usb), "USB: %s",
				status == STATUS_CONNECTED ?
				"connected" : "disconnected");
	if (saved_usb_status != status) {
		RESET_ALARM;
		lcd_state = (lcd_state_t)lcd_state(LCD_USB_EVENT);
		saved_usb_status = status;
	}

	status = charger_status();
	if (status == STATUS_UNKOWN)
		snprintf(buf_charger, sizeof(buf_charger), "Charger: Unknow");
	else
		snprintf(buf_charger, sizeof(buf_charger), "Charger: %s",
				status == STATUS_CONNECTED ?
				"connected" : "disconnected");
	if (saved_charger_status != status) {
		RESET_ALARM;
		lcd_state = (lcd_state_t)lcd_state(LCD_USB_EVENT);
		saved_charger_status = status;
	}

	status = battery_capacity();
	if (status < 0)
		snprintf(buf_bat, sizeof(buf_bat), "Battery: Unknow");
	else
		snprintf(buf_bat, sizeof(buf_bat), "Battery: %d%%", status);

	tboot_ui_uevent("%s, %s, %s", buf_usb, buf_charger, buf_bat);
}