root / branches / simulator / projects / libdragonfly / battery.c @ 891
History | View | Annotate | Download (2.41 KB)
1 |
/**
|
---|---|
2 |
* Copyright (c) 2007 Colony Project
|
3 |
*
|
4 |
* Permission is hereby granted, free of charge, to any person
|
5 |
* obtaining a copy of this software and associated documentation
|
6 |
* files (the "Software"), to deal in the Software without
|
7 |
* restriction, including without limitation the rights to use,
|
8 |
* copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9 |
* copies of the Software, and to permit persons to whom the
|
10 |
* Software is furnished to do so, subject to the following
|
11 |
* conditions:
|
12 |
*
|
13 |
* The above copyright notice and this permission notice shall be
|
14 |
* included in all copies or substantial portions of the Software.
|
15 |
*
|
16 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17 |
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
18 |
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19 |
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
20 |
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
21 |
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
22 |
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
23 |
* OTHER DEALINGS IN THE SOFTWARE.
|
24 |
**/
|
25 |
|
26 |
/**
|
27 |
* @file battery.c
|
28 |
* @brief Implementation for reading battery level
|
29 |
*
|
30 |
* Contains functions for reading the battery level.
|
31 |
*
|
32 |
* @author Colony Project, CMU Robotics Club
|
33 |
**/
|
34 |
|
35 |
#include "battery.h" |
36 |
#include "analog.h" |
37 |
|
38 |
//Constants for Battery
|
39 |
//Constants determined experimentally 2/9/2007 - James Kong
|
40 |
//See Battery8_data.xls for experiment data
|
41 |
#define BATTERY_NEXT_READ_PRESCALAR 5 |
42 |
#define MAX_LOW_COUNT 5 |
43 |
|
44 |
//global variables
|
45 |
int battery_low_count;
|
46 |
|
47 |
int battery8(void) |
48 |
{ |
49 |
return analog8(BATT_PORT);
|
50 |
} |
51 |
|
52 |
int battery(void) |
53 |
{ |
54 |
/* 5 volts is the max, 255 is the max 8bit number , *2 for the divider */
|
55 |
return analog8(BATT_PORT) * 500 >>7; |
56 |
} |
57 |
|
58 |
|
59 |
char battery_low(void) |
60 |
{ |
61 |
static char next_read = 0; |
62 |
static char ret = 0; |
63 |
|
64 |
int batt_reading = battery8();
|
65 |
|
66 |
if(next_read == 0) |
67 |
{ |
68 |
if(batt_reading > BATTERY_LOWV)
|
69 |
{ |
70 |
ret = 0;
|
71 |
battery_low_count = 0;
|
72 |
} |
73 |
else
|
74 |
{ |
75 |
battery_low_count++; |
76 |
if( battery_low_count >= MAX_LOW_COUNT )
|
77 |
{ |
78 |
ret = 1;
|
79 |
battery_low_count = 0;
|
80 |
} |
81 |
} |
82 |
|
83 |
next_read = BATTERY_NEXT_READ_PRESCALAR; |
84 |
} |
85 |
else next_read--;
|
86 |
|
87 |
return ret;
|
88 |
} |
89 |
|
90 |
int battery8_avg(int n_samples) |
91 |
{ |
92 |
int i;
|
93 |
long int sum = 0; |
94 |
|
95 |
for(i = 0; i < n_samples; i++) |
96 |
{ |
97 |
sum += battery8(); |
98 |
} |
99 |
|
100 |
return (int)(sum / n_samples); |
101 |
} |
102 |
|