The field version of the solarnano grid on the ionQubes
Fork of SolarNanoGridv3 by
Battery/Battery.h@36:a5620262f296, 2016-09-06 (annotated)
- Committer:
- defrost
- Date:
- Tue Sep 06 06:08:56 2016 +0000
- Revision:
- 36:a5620262f296
- Parent:
- 31:3bf76548e024
Turned off the charge rate update from hub
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
defrost | 5:57b06b4b47c6 | 1 | /** |
defrost | 5:57b06b4b47c6 | 2 | *@section DESCRIPTION |
defrost | 5:57b06b4b47c6 | 3 | * mbed SolarNanogrid Battery. |
defrost | 5:57b06b4b47c6 | 4 | *@section LICENSE |
defrost | 5:57b06b4b47c6 | 5 | * Copyright (c) 2016, Malcolm McCulloch |
defrost | 5:57b06b4b47c6 | 6 | * |
defrost | 5:57b06b4b47c6 | 7 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
defrost | 5:57b06b4b47c6 | 8 | * of this software and associated documentation files (the "Software"), to deal |
defrost | 5:57b06b4b47c6 | 9 | * in the Software without restriction, including without limitation the rights |
defrost | 5:57b06b4b47c6 | 10 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
defrost | 5:57b06b4b47c6 | 11 | * copies of the Software, and to permit persons to whom the Software is |
defrost | 5:57b06b4b47c6 | 12 | * furnished to do so, subject to the following conditions: |
defrost | 5:57b06b4b47c6 | 13 | * |
defrost | 5:57b06b4b47c6 | 14 | * The above copyright notice and this permission notice shall be included in |
defrost | 5:57b06b4b47c6 | 15 | * all copies or substantial portions of the Software. |
defrost | 5:57b06b4b47c6 | 16 | * |
defrost | 5:57b06b4b47c6 | 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
defrost | 5:57b06b4b47c6 | 18 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
defrost | 5:57b06b4b47c6 | 19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
defrost | 5:57b06b4b47c6 | 20 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
defrost | 5:57b06b4b47c6 | 21 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
defrost | 5:57b06b4b47c6 | 22 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
defrost | 5:57b06b4b47c6 | 23 | * THE SOFTWARE. |
defrost | 5:57b06b4b47c6 | 24 | * @file "Battery.h" |
defrost | 5:57b06b4b47c6 | 25 | */ |
defrost | 5:57b06b4b47c6 | 26 | |
defrost | 5:57b06b4b47c6 | 27 | #ifndef BATTERY_H |
defrost | 5:57b06b4b47c6 | 28 | #define BATTERY_H |
defrost | 36:a5620262f296 | 29 | |
defrost | 36:a5620262f296 | 30 | //#define USEHUBCHARGERATE // define this to use charge rate broadcast by the hub |
defrost | 36:a5620262f296 | 31 | |
defrost | 5:57b06b4b47c6 | 32 | #include <mbed.h> |
defrost | 5:57b06b4b47c6 | 33 | #include "NRF2401P.h" |
defrost | 5:57b06b4b47c6 | 34 | #include "nRF24l01.h" |
defrost | 5:57b06b4b47c6 | 35 | #include "SDFileSystem.h" |
defrost | 5:57b06b4b47c6 | 36 | #include "SolarNanoGrid.h" |
defrost | 10:30c9e8df0032 | 37 | |
defrost | 5:57b06b4b47c6 | 38 | /** \brief The battery logs data and interacts with the locker. |
defrost | 5:57b06b4b47c6 | 39 | * |
defrost | 5:57b06b4b47c6 | 40 | * The config.ini must start with B |
defrost | 5:57b06b4b47c6 | 41 | * |
defrost | 5:57b06b4b47c6 | 42 | */ |
defrost | 5:57b06b4b47c6 | 43 | class Battery : public SolarNanoGrid |
defrost | 5:57b06b4b47c6 | 44 | { |
defrost | 5:57b06b4b47c6 | 45 | public: |
defrost | 5:57b06b4b47c6 | 46 | // ********************* |
defrost | 5:57b06b4b47c6 | 47 | // * Public variables: * |
defrost | 5:57b06b4b47c6 | 48 | // ********************* |
adrienBiz | 8:0acda4f2e0a8 | 49 | char* logDir; |
adrienBiz | 8:0acda4f2e0a8 | 50 | float maxChargeRate; // Charge rate in amps |
defrost | 5:57b06b4b47c6 | 51 | |
defrost | 5:57b06b4b47c6 | 52 | // ********************* |
defrost | 5:57b06b4b47c6 | 53 | // * Public functions: * |
defrost | 5:57b06b4b47c6 | 54 | // ********************* |
defrost | 5:57b06b4b47c6 | 55 | |
defrost | 5:57b06b4b47c6 | 56 | // Constructor: |
epgmdm | 11:87ab310924f0 | 57 | Battery(FILE* fp, Serial *pc); |
defrost | 5:57b06b4b47c6 | 58 | |
defrost | 5:57b06b4b47c6 | 59 | // Main loop function: |
defrost | 13:de43f28c0365 | 60 | virtual void loop(void); |
defrost | 5:57b06b4b47c6 | 61 | /** |
defrost | 5:57b06b4b47c6 | 62 | * Checks in the battery to the locker |
defrost | 5:57b06b4b47c6 | 63 | * Transmits "C ID" |
defrost | 5:57b06b4b47c6 | 64 | */ |
defrost | 5:57b06b4b47c6 | 65 | void checkIn(); |
defrost | 5:57b06b4b47c6 | 66 | |
epgmdm | 12:d9b13f31206d | 67 | /** |
defrost | 17:dc19b3b39790 | 68 | * Returns the max current that can be drawn from the system, in amps |
defrost | 17:dc19b3b39790 | 69 | */ |
epgmdm | 12:d9b13f31206d | 70 | |
defrost | 17:dc19b3b39790 | 71 | float getMaxCurrent(); |
epgmdm | 12:d9b13f31206d | 72 | |
defrost | 17:dc19b3b39790 | 73 | /** |
defrost | 17:dc19b3b39790 | 74 | * returns a string of the log directory |
defrost | 17:dc19b3b39790 | 75 | */ |
defrost | 17:dc19b3b39790 | 76 | char *getLogDirectory(); |
epgmdm | 18:be77ad141fac | 77 | |
defrost | 17:dc19b3b39790 | 78 | /** |
defrost | 17:dc19b3b39790 | 79 | * Returns true if we are authorized to start charging. |
defrost | 17:dc19b3b39790 | 80 | */ |
defrost | 17:dc19b3b39790 | 81 | bool startCharging(void); |
epgmdm | 18:be77ad141fac | 82 | |
defrost | 17:dc19b3b39790 | 83 | /** |
defrost | 17:dc19b3b39790 | 84 | * Returns true if we are checked in. |
defrost | 17:dc19b3b39790 | 85 | */ |
epgmdm | 19:1a5447ecbda0 | 86 | bool isCheckedIn(void); |
defrost | 23:56134ab060f2 | 87 | |
defrost | 21:cb912a342846 | 88 | /** |
defrost | 21:cb912a342846 | 89 | * sends a request for a charge update. |
defrost | 21:cb912a342846 | 90 | */ |
defrost | 21:cb912a342846 | 91 | void requestMaxCharge(); |
epgmdm | 30:47f73b850a5e | 92 | |
defrost | 24:6aba7f16e0c6 | 93 | /** |
defrost | 24:6aba7f16e0c6 | 94 | * Disables all of the interrupts, for a proper sleep state. |
defrost | 24:6aba7f16e0c6 | 95 | */ |
defrost | 24:6aba7f16e0c6 | 96 | void disableInt(void); |
defrost | 23:56134ab060f2 | 97 | |
defrost | 5:57b06b4b47c6 | 98 | protected: |
defrost | 5:57b06b4b47c6 | 99 | // ********************** |
defrost | 5:57b06b4b47c6 | 100 | // * Protected variables: * |
defrost | 5:57b06b4b47c6 | 101 | // ********************** |
defrost | 5:57b06b4b47c6 | 102 | /** |
defrost | 5:57b06b4b47c6 | 103 | * Name of log directory, include last / |
defrost | 5:57b06b4b47c6 | 104 | */ |
defrost | 5:57b06b4b47c6 | 105 | |
adrienBiz | 8:0acda4f2e0a8 | 106 | char count; |
adrienBiz | 8:0acda4f2e0a8 | 107 | char* txFileName; |
defrost | 5:57b06b4b47c6 | 108 | |
adrienBiz | 8:0acda4f2e0a8 | 109 | char* fullTxFilePath; |
defrost | 5:57b06b4b47c6 | 110 | |
adrienBiz | 8:0acda4f2e0a8 | 111 | int sizeRead; |
adrienBiz | 8:0acda4f2e0a8 | 112 | unsigned int fileLength; |
adrienBiz | 8:0acda4f2e0a8 | 113 | unsigned int filePointer; |
adrienBiz | 8:0acda4f2e0a8 | 114 | unsigned int fileLeft; |
adrienBiz | 8:0acda4f2e0a8 | 115 | char *sdBuffer; |
adrienBiz | 8:0acda4f2e0a8 | 116 | unsigned int sdBuffPnt; |
defrost | 5:57b06b4b47c6 | 117 | /** |
defrost | 5:57b06b4b47c6 | 118 | * Open channel address of the locker |
defrost | 5:57b06b4b47c6 | 119 | */ |
adrienBiz | 8:0acda4f2e0a8 | 120 | long long openAddr; |
defrost | 5:57b06b4b47c6 | 121 | |
defrost | 5:57b06b4b47c6 | 122 | /** |
defrost | 5:57b06b4b47c6 | 123 | * Private channel address of the locker |
defrost | 5:57b06b4b47c6 | 124 | */ |
adrienBiz | 8:0acda4f2e0a8 | 125 | long long privateAddr; |
defrost | 5:57b06b4b47c6 | 126 | |
adrienBiz | 8:0acda4f2e0a8 | 127 | unsigned int countSD; |
defrost | 5:57b06b4b47c6 | 128 | |
defrost | 5:57b06b4b47c6 | 129 | // ********************** |
defrost | 5:57b06b4b47c6 | 130 | // * Protected flags: * |
defrost | 5:57b06b4b47c6 | 131 | // ********************** |
adrienBiz | 8:0acda4f2e0a8 | 132 | volatile char * flag; |
adrienBiz | 8:0acda4f2e0a8 | 133 | volatile char checkedIn; |
adrienBiz | 8:0acda4f2e0a8 | 134 | volatile char flagGetTime; |
adrienBiz | 8:0acda4f2e0a8 | 135 | volatile char flagNextFile; |
epgmdm | 18:be77ad141fac | 136 | volatile char flagHubNotReady; /**< True if flagStartFileTx and MaxRT */ |
epgmdm | 18:be77ad141fac | 137 | volatile char flagStartFileTx; /**< Start of file transmission. 0 when all files done */ |
epgmdm | 18:be77ad141fac | 138 | volatile char flagDirFileName; |
adrienBiz | 8:0acda4f2e0a8 | 139 | volatile char flagSendFileName; |
adrienBiz | 8:0acda4f2e0a8 | 140 | volatile char flagSendFileSize; |
adrienBiz | 8:0acda4f2e0a8 | 141 | volatile char flagSendFile; |
adrienBiz | 8:0acda4f2e0a8 | 142 | volatile char flagSendFileDone; |
epgmdm | 12:d9b13f31206d | 143 | volatile char flagGetMaxCharge; |
epgmdm | 12:d9b13f31206d | 144 | volatile char sendingFile; |
defrost | 17:dc19b3b39790 | 145 | volatile bool startCharge; |
epgmdm | 18:be77ad141fac | 146 | volatile char flagCheckOut; |
defrost | 28:e85a86fc8b59 | 147 | volatile char sending; |
defrost | 5:57b06b4b47c6 | 148 | |
defrost | 5:57b06b4b47c6 | 149 | // ********************** |
defrost | 5:57b06b4b47c6 | 150 | // * Protected interrupts: * |
defrost | 5:57b06b4b47c6 | 151 | // ********************** |
adrienBiz | 8:0acda4f2e0a8 | 152 | InterruptIn* button; |
adrienBiz | 8:0acda4f2e0a8 | 153 | Ticker* txWatch; |
adrienBiz | 8:0acda4f2e0a8 | 154 | Timeout* delay; |
defrost | 5:57b06b4b47c6 | 155 | |
defrost | 5:57b06b4b47c6 | 156 | // ********************** |
defrost | 5:57b06b4b47c6 | 157 | // * Protected functions: * |
defrost | 5:57b06b4b47c6 | 158 | // ********************** |
defrost | 5:57b06b4b47c6 | 159 | /** |
defrost | 5:57b06b4b47c6 | 160 | * Checks in the battery to the locker |
defrost | 5:57b06b4b47c6 | 161 | * Acknowledge received. |
defrost | 5:57b06b4b47c6 | 162 | * Changes the TX pipe address to private |
defrost | 5:57b06b4b47c6 | 163 | * |
defrost | 5:57b06b4b47c6 | 164 | */ |
defrost | 5:57b06b4b47c6 | 165 | void doCheckIn(); |
defrost | 5:57b06b4b47c6 | 166 | /** |
defrost | 5:57b06b4b47c6 | 167 | * Checks out the battery from the locker |
defrost | 5:57b06b4b47c6 | 168 | * Acknowledge received. |
defrost | 5:57b06b4b47c6 | 169 | * Changes the TX pipe address to open |
defrost | 5:57b06b4b47c6 | 170 | * |
defrost | 5:57b06b4b47c6 | 171 | */ |
defrost | 5:57b06b4b47c6 | 172 | void doCheckOut(); |
defrost | 5:57b06b4b47c6 | 173 | /** |
defrost | 5:57b06b4b47c6 | 174 | * sends a request for time update. |
defrost | 5:57b06b4b47c6 | 175 | */ |
epgmdm | 12:d9b13f31206d | 176 | void requestTime(); |
defrost | 5:57b06b4b47c6 | 177 | /** |
defrost | 5:57b06b4b47c6 | 178 | * Sets the value pointed to by flag to 1. |
defrost | 5:57b06b4b47c6 | 179 | */ |
defrost | 5:57b06b4b47c6 | 180 | |
defrost | 5:57b06b4b47c6 | 181 | void nextFileToTx(); |
defrost | 5:57b06b4b47c6 | 182 | void sendDirName(); |
defrost | 5:57b06b4b47c6 | 183 | void sendFileName(); |
defrost | 5:57b06b4b47c6 | 184 | void sendFileSize(); |
defrost | 5:57b06b4b47c6 | 185 | void sendFile(); |
defrost | 5:57b06b4b47c6 | 186 | void sendFileDone(); |
defrost | 5:57b06b4b47c6 | 187 | void setFlag(); |
epgmdm | 18:be77ad141fac | 188 | |
epgmdm | 18:be77ad141fac | 189 | /** |
epgmdm | 18:be77ad141fac | 190 | * Checks out. Sends O %d, ID to locker |
epgmdm | 18:be77ad141fac | 191 | * |
epgmdm | 18:be77ad141fac | 192 | */ |
epgmdm | 18:be77ad141fac | 193 | void checkOut(); |
defrost | 5:57b06b4b47c6 | 194 | /** |
defrost | 5:57b06b4b47c6 | 195 | * Retransmits the last data. |
defrost | 5:57b06b4b47c6 | 196 | * |
defrost | 5:57b06b4b47c6 | 197 | */ |
defrost | 5:57b06b4b47c6 | 198 | void retransmit(); |
defrost | 5:57b06b4b47c6 | 199 | |
defrost | 5:57b06b4b47c6 | 200 | |
defrost | 5:57b06b4b47c6 | 201 | /** |
defrost | 5:57b06b4b47c6 | 202 | * called when the nrf the button is pressed |
defrost | 5:57b06b4b47c6 | 203 | * |
defrost | 5:57b06b4b47c6 | 204 | */ |
defrost | 5:57b06b4b47c6 | 205 | void intButton(); |
defrost | 5:57b06b4b47c6 | 206 | /** |
defrost | 5:57b06b4b47c6 | 207 | * called when the nrf creates an interrupt. |
defrost | 5:57b06b4b47c6 | 208 | * |
defrost | 5:57b06b4b47c6 | 209 | */ |
defrost | 5:57b06b4b47c6 | 210 | void intNrf(); |
epgmdm | 31:3bf76548e024 | 211 | void resetPackets(); |
epgmdm | 30:47f73b850a5e | 212 | /** |
epgmdm | 30:47f73b850a5e | 213 | * Gathers a packet from the file defined by fullTxFilePath |
epgmdm | 30:47f73b850a5e | 214 | * @param readOnly if true then it does not move the internal pointers on. |
epgmdm | 30:47f73b850a5e | 215 | */ |
epgmdm | 30:47f73b850a5e | 216 | char* getPacket(unsigned int *sizePackett,char * fullTxFilePath); |
defrost | 5:57b06b4b47c6 | 217 | |
epgmdm | 12:d9b13f31206d | 218 | |
defrost | 5:57b06b4b47c6 | 219 | private: |
defrost | 5:57b06b4b47c6 | 220 | // ********************** |
defrost | 5:57b06b4b47c6 | 221 | // * Private variables: * |
defrost | 5:57b06b4b47c6 | 222 | // ********************** |
defrost | 5:57b06b4b47c6 | 223 | |
adrienBiz | 8:0acda4f2e0a8 | 224 | volatile bool maxRT; |
defrost | 5:57b06b4b47c6 | 225 | |
epgmdm | 12:d9b13f31206d | 226 | |
defrost | 5:57b06b4b47c6 | 227 | }; |
defrost | 5:57b06b4b47c6 | 228 | #endif |