Polls the voltage of an array of solar panels periodically user-defined times.

Committer:
yotammos
Date:
Tue Nov 01 21:33:23 2016 +0000
Revision:
0:3cbc1a6e087c
Version 1.0

Who changed what in which revision?

UserRevisionLine numberNew contents of line
yotammos 0:3cbc1a6e087c 1 #ifndef SOLAR_H
yotammos 0:3cbc1a6e087c 2 #define SOLAR_H
yotammos 0:3cbc1a6e087c 3
yotammos 0:3cbc1a6e087c 4 #include "mbed.h"
yotammos 0:3cbc1a6e087c 5 #include <string>
yotammos 0:3cbc1a6e087c 6 #include <stdlib.h>
yotammos 0:3cbc1a6e087c 7 #include <vector>
yotammos 0:3cbc1a6e087c 8
yotammos 0:3cbc1a6e087c 9 class solarArray {
yotammos 0:3cbc1a6e087c 10 private:
yotammos 0:3cbc1a6e087c 11 AnalogIn* panels;
yotammos 0:3cbc1a6e087c 12 unsigned* pins;
yotammos 0:3cbc1a6e087c 13 vector<float>* data; // a data vector for each panel
yotammos 0:3cbc1a6e087c 14 size_t size; // up to 6 solar panels
yotammos 0:3cbc1a6e087c 15
yotammos 0:3cbc1a6e087c 16 private:
yotammos 0:3cbc1a6e087c 17 void allocatePanels();
yotammos 0:3cbc1a6e087c 18 void samplingCycle(unsigned duration, float samplingPeriod);
yotammos 0:3cbc1a6e087c 19 void sample();
yotammos 0:3cbc1a6e087c 20
yotammos 0:3cbc1a6e087c 21 public:
yotammos 0:3cbc1a6e087c 22 solarArray(); // default ctor
yotammos 0:3cbc1a6e087c 23 solarArray(solarArray&); // copy ctor
yotammos 0:3cbc1a6e087c 24 solarArray(size_t numPanels, unsigned* pinNums);
yotammos 0:3cbc1a6e087c 25 ~solarArray();
yotammos 0:3cbc1a6e087c 26
yotammos 0:3cbc1a6e087c 27 void gatherInfo_days(unsigned numDays, unsigned start, unsigned duration ,unsigned samplingPeriod);
yotammos 0:3cbc1a6e087c 28 void gatherInfo_secs(unsigned start, unsigned duration, unsigned samplingPeriod);
yotammos 0:3cbc1a6e087c 29 vector<float> getPanelData(unsigned panel);
yotammos 0:3cbc1a6e087c 30 };
yotammos 0:3cbc1a6e087c 31
yotammos 0:3cbc1a6e087c 32 solarArray::solarArray() : panels(NULL), pins(NULL),size(0)
yotammos 0:3cbc1a6e087c 33 {
yotammos 0:3cbc1a6e087c 34 }
yotammos 0:3cbc1a6e087c 35
yotammos 0:3cbc1a6e087c 36 void solarArray::allocatePanels() {
yotammos 0:3cbc1a6e087c 37 for (int i = 0; i < size; i++) {
yotammos 0:3cbc1a6e087c 38 if (pins[i] == 15)
yotammos 0:3cbc1a6e087c 39 new (panels + i) AnalogIn(p15);
yotammos 0:3cbc1a6e087c 40 else if (pins[i] == 16)
yotammos 0:3cbc1a6e087c 41 new (panels + i) AnalogIn(p16);
yotammos 0:3cbc1a6e087c 42 else if (pins[i] == 17)
yotammos 0:3cbc1a6e087c 43 new (panels + i) AnalogIn(p17);
yotammos 0:3cbc1a6e087c 44 else if (pins[i] == 18)
yotammos 0:3cbc1a6e087c 45 new (panels + i) AnalogIn(p18);
yotammos 0:3cbc1a6e087c 46 else if (pins[i] == 19)
yotammos 0:3cbc1a6e087c 47 new (panels + i) AnalogIn(p19);
yotammos 0:3cbc1a6e087c 48 else if (pins[i] == 20)
yotammos 0:3cbc1a6e087c 49 new (panels + i) AnalogIn(p20);
yotammos 0:3cbc1a6e087c 50 }
yotammos 0:3cbc1a6e087c 51 }
yotammos 0:3cbc1a6e087c 52
yotammos 0:3cbc1a6e087c 53 solarArray::solarArray(solarArray& rhs) : size(rhs.size)
yotammos 0:3cbc1a6e087c 54 {
yotammos 0:3cbc1a6e087c 55 panels = (AnalogIn*) malloc(sizeof(AnalogIn)*size);
yotammos 0:3cbc1a6e087c 56 pins = (unsigned*) malloc(sizeof(unsigned)*size);
yotammos 0:3cbc1a6e087c 57
yotammos 0:3cbc1a6e087c 58 for (int i = 0; i < size; i++)
yotammos 0:3cbc1a6e087c 59 new (pins + i) unsigned(rhs.pins[i]);
yotammos 0:3cbc1a6e087c 60
yotammos 0:3cbc1a6e087c 61 allocatePanels();
yotammos 0:3cbc1a6e087c 62 }
yotammos 0:3cbc1a6e087c 63
yotammos 0:3cbc1a6e087c 64 solarArray::solarArray(size_t numPanels, unsigned* pinNums) : size(numPanels)
yotammos 0:3cbc1a6e087c 65 {
yotammos 0:3cbc1a6e087c 66 panels = (AnalogIn*) malloc(sizeof(AnalogIn)*size);
yotammos 0:3cbc1a6e087c 67 pins = (unsigned*) malloc(sizeof(unsigned)*size);
yotammos 0:3cbc1a6e087c 68
yotammos 0:3cbc1a6e087c 69 for (int i = 0; i < size; i++)
yotammos 0:3cbc1a6e087c 70 new (pins + i) unsigned(pinNums[i]);
yotammos 0:3cbc1a6e087c 71
yotammos 0:3cbc1a6e087c 72 allocatePanels();
yotammos 0:3cbc1a6e087c 73 }
yotammos 0:3cbc1a6e087c 74
yotammos 0:3cbc1a6e087c 75 solarArray::~solarArray()
yotammos 0:3cbc1a6e087c 76 {
yotammos 0:3cbc1a6e087c 77 for (int i = 0; i < size; i++)
yotammos 0:3cbc1a6e087c 78 (panels + i)->~AnalogIn();
yotammos 0:3cbc1a6e087c 79
yotammos 0:3cbc1a6e087c 80 if (panels != NULL && size > 0)
yotammos 0:3cbc1a6e087c 81 free(panels);
yotammos 0:3cbc1a6e087c 82
yotammos 0:3cbc1a6e087c 83 for (int i = 0; i < size; i++)
yotammos 0:3cbc1a6e087c 84 (pins + i)->~unsigned();
yotammos 0:3cbc1a6e087c 85
yotammos 0:3cbc1a6e087c 86 if (pins != NULL && size > 0)
yotammos 0:3cbc1a6e087c 87 free(pins);
yotammos 0:3cbc1a6e087c 88 }
yotammos 0:3cbc1a6e087c 89
yotammos 0:3cbc1a6e087c 90 vector<float> solarArray::getPanelData(unsigned panel)
yotammos 0:3cbc1a6e087c 91 {
yotammos 0:3cbc1a6e087c 92 return data[panel];
yotammos 0:3cbc1a6e087c 93 }
yotammos 0:3cbc1a6e087c 94
yotammos 0:3cbc1a6e087c 95 void solarArray::sample() {
yotammos 0:3cbc1a6e087c 96 for (int i = 0; i < size; i++)
yotammos 0:3cbc1a6e087c 97 data[i].push_back(panels[i]);
yotammos 0:3cbc1a6e087c 98 }
yotammos 0:3cbc1a6e087c 99
yotammos 0:3cbc1a6e087c 100 void solarArray::samplingCycle(unsigned duration, float samplingPeriod) {
yotammos 0:3cbc1a6e087c 101 //Ticker t;
yotammos 0:3cbc1a6e087c 102
yotammos 0:3cbc1a6e087c 103 data = (vector<float>*) malloc(sizeof(vector<float>)*size);
yotammos 0:3cbc1a6e087c 104
yotammos 0:3cbc1a6e087c 105 for (int i = 0; i < size; i++)
yotammos 0:3cbc1a6e087c 106 new (data + i) vector<float>;
yotammos 0:3cbc1a6e087c 107
yotammos 0:3cbc1a6e087c 108 //t.attach(this::sample, samplingPeriod);
yotammos 0:3cbc1a6e087c 109 for (float i = 0; i < duration; i += samplingPeriod) {
yotammos 0:3cbc1a6e087c 110 for (int j = 0; j < size; j++)
yotammos 0:3cbc1a6e087c 111 data[j].push_back(panels[j]*3.3);
yotammos 0:3cbc1a6e087c 112
yotammos 0:3cbc1a6e087c 113 wait(samplingPeriod);
yotammos 0:3cbc1a6e087c 114 }
yotammos 0:3cbc1a6e087c 115 //wait(duration);
yotammos 0:3cbc1a6e087c 116 }
yotammos 0:3cbc1a6e087c 117
yotammos 0:3cbc1a6e087c 118 void solarArray::gatherInfo_days(unsigned numDays, unsigned start, unsigned duration ,unsigned samplingPeriod)
yotammos 0:3cbc1a6e087c 119 {
yotammos 0:3cbc1a6e087c 120 Ticker t;
yotammos 0:3cbc1a6e087c 121
yotammos 0:3cbc1a6e087c 122 for (int i = 0; i < numDays; i++) {
yotammos 0:3cbc1a6e087c 123 wait(start);
yotammos 0:3cbc1a6e087c 124
yotammos 0:3cbc1a6e087c 125 samplingCycle(duration, samplingPeriod);
yotammos 0:3cbc1a6e087c 126
yotammos 0:3cbc1a6e087c 127 wait(3600*24 - start - duration); // wait until the next day
yotammos 0:3cbc1a6e087c 128 }
yotammos 0:3cbc1a6e087c 129 }
yotammos 0:3cbc1a6e087c 130
yotammos 0:3cbc1a6e087c 131 void solarArray::gatherInfo_secs(unsigned start, unsigned duration,unsigned samplingPeriod) {
yotammos 0:3cbc1a6e087c 132 wait(start);
yotammos 0:3cbc1a6e087c 133 samplingCycle(duration, samplingPeriod);
yotammos 0:3cbc1a6e087c 134 }
yotammos 0:3cbc1a6e087c 135
yotammos 0:3cbc1a6e087c 136 #endif