Statistics
| Revision:

root / demos / hunter_prey / lib / src / libdragonfly / battery.c @ 1828

History | View | Annotate | Download (3.78 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
/**
28
 * @file battery.c
29
 * @brief Implementation for reading battery level
30
 *
31
 * Contains functions for reading the battery level.
32
 *
33
 * @author Colony Project, CMU Robotics Club
34
 **/
35

    
36
#include "battery.h"
37
#include "analog.h"
38

    
39
//Constants for Battery
40
//Constants determined experimentally 2/9/2007 - James Kong
41
//See Battery8_data.xls for experiment data
42
#define BATTERY_NEXT_READ_PRESCALAR 5
43
#define MAX_LOW_COUNT 5
44

    
45
//global variables
46
int battery_low_count;
47

    
48
/**
49
 * @defgroup battery Battery
50
 * @brief Functions for reading battery voltage.
51
 * 
52
 * Contains functions for checking the current
53
 * voltage of the battery. Include battery.h to
54
 * access these functions.
55
 *
56
 * @{
57
 **/
58

    
59
/**
60
 * Returns the voltage of the battery as an analog8
61
 * reading. 128 is approximately 5 volts. analog_init
62
 * must be called before using this function.
63
 *
64
 * @return the voltage of the battery as an analog8
65
 * reading
66
 *
67
 * @see analog_init, battery, analog8
68
 **/
69
int battery8(void)
70
{
71
  return analog8(BATT_PORT);
72
}
73

    
74
/**
75
 * Returns the voltage of the battery in deciVolts.
76
 * analog_init must be called before using this function.
77
 *
78
 * @return the voltage of the battery in deciVolts.
79
 *
80
 * @see analog_init, battery8
81
 **/
82
int battery(void)
83
{
84
  /* 5 volts is the max, 255 is the max 8bit number , *2 for the divider */
85
        return analog8(BATT_PORT) * 500 >>7;
86
}
87

    
88

    
89
/**
90
 * Checks if the battery is low. analog_init must be called before
91
 * this function can be used. This function waits for several low
92
 * battery readings in a row to avoid false positives.
93
 *
94
 * @return 1 if the battery is low, 0 otherwise
95
 *
96
 * @see analog_init
97
 **/
98
char battery_low(void)
99
{
100
  static char next_read = 0;
101
  static char ret = 0;
102
  
103
  int batt_reading = battery8();
104

    
105
  if(next_read == 0) 
106
  {
107
    if(batt_reading > BATTERY_LOWV)
108
    {
109
        ret = 0;
110
        battery_low_count = 0;
111
    }
112
    else
113
    {
114
        battery_low_count++;
115
        if( battery_low_count >= MAX_LOW_COUNT )
116
        {
117
            ret = 1;
118
            battery_low_count = 0;
119
        }
120
    }
121
    
122
    next_read = BATTERY_NEXT_READ_PRESCALAR;
123
  }
124
  else next_read--;
125
  
126
  return ret;
127
}
128

    
129
/**
130
 * Averages n_samples battery8 readings. This function may
131
 * take a while to complete, and is only made available
132
 * for convenience. analog_init must be called before this
133
 * function may be used.
134
 *
135
 * @param n_samples the number of times to sample the battery voltage
136
 *
137
 * @return the average reading for the battery voltage, where 128
138
 * is approximately 5 Volts.
139
 *
140
 * @see analog_init, battery
141
 **/
142
int battery8_avg(int n_samples)
143
{
144
  int i;
145
  long int sum = 0;
146
  
147
  for(i = 0; i < n_samples; i++) 
148
  {
149
    sum += battery8();
150
  }
151
  
152
  return (int)(sum / n_samples);
153
}
154

    
155
/** @} **/ //end defgroup
156