BayEOSArduino Library
DistanceShield.h
1 /*
2  * Just a bunch of variables and functions to
3  * handle measurements on UniversalShield
4  *
5  * expects to have a bayeosClient called "client"
6  * and a RTC called "myRTC"
7  channel 1 - CH-Status (0 sleeping - 1 charging - 2 done - 3 error)
8  channel 2 U_LiPo (V)
9  channel 3+4 - uptime/cpu-time (s)
10  channel 5 - RTC-Temperature (°C)
11  channel 6 D1 (V)
12  channel 7 D2 (V)
13  channel 8 D3 (V)
14  channel 9 D4 (V)
15 
16  */
17 
18 #define UPSTEPPER_EN_PIN A3
19 
20 #define MCP_ADDR_OFFSET 1
21 
22 #define XBEE_SLEEP_PIN 5
23 float tmp_float ;
24 
25 unsigned long start_time;
26 unsigned long last_data;
27 volatile uint8_t wdcount=0; //set 0 in loop otherwise board will call a reset!!
28 
29 MCP342x mcp = MCP342x();
30 
31 #ifndef HMPBOX
32 #define HMPBOX 0
33 #endif
34 
35 void measure(void){
36  last_data=myRTC.now().get();
37  //send buffered frames
38  //only one each time
39  myRTC.convertTemperature();
40  digitalWrite(UPSTEPPER_EN_PIN,HIGH);
41  delay(50);
42  //Start a new data frame
43  client.startDataFrame(BayEOS_Float32le);
44  client.addToPayload((uint8_t) 0); //Offset 0
45  // Charge
46  analogReference(INTERNAL);
47  tmp_float=analogRead(A6);
48  /*
49  * >900 sleeping
50  * >550 charging
51  * >350 done
52  * <350 error
53  */
54  if(tmp_float>900) tmp_float=0;
55  else if(tmp_float>550) tmp_float=1;
56  else if(tmp_float>350) tmp_float=2;
57  else tmp_float=3;
58 
59  client.addToPayload(tmp_float); // Float
60  tmp_float=(1.1 / 1024)*analogRead(A7)*(10+2)/2; //Voltage
61  /*
62  * voltage = tmp_float * (1.1 / 1024)* (10+2)/2; //Voltage divider
63  */
64  client.addToPayload(tmp_float); // Float
65 
66  client.addToPayload((float) (last_data-start_time));
67  client.addToPayload((float) (millis()/1000));
68  client.addToPayload((float) myRTC.getTemperature());
69 
70  if(HMPBOX) delay(2000);
71 
72  //Read MCP
73  for(uint8_t i=0;i<4;i++){
74  mcp.setConf(MCP_ADDR_OFFSET,1,i,0,0,0);
75  delay(10);
76  tmp_float = (i<2 && HMPBOX?1:2)*mcp.getData(MCP_ADDR_OFFSET);
77  client.addToPayload(tmp_float); // Float
78  }
79  digitalWrite(UPSTEPPER_EN_PIN,LOW);
80 
81 }
82 
83 //****************************************************************
84 // Watchdog Interrupt Service / is executed when watchdog timed out
85 // You have to set this! Otherwise it will call reset!
86 ISR(WDT_vect) {
87  wdcount++;
88  if(wdcount>240){
89  asm volatile (" jmp 0"); //restart programm
90  }
91 }
92 
93 
94 void initShield(void){
95  Wire.begin();
96  myRTC.begin();
97  pinMode(UPSTEPPER_EN_PIN, OUTPUT);
98  digitalWrite(UPSTEPPER_EN_PIN,LOW);
99  start_time=myRTC.now().get();
100 }
101 
102 void swap(float *x,float *y)
103 {
104  float temp;
105  temp = *x;
106  *x = *y;
107  *y = temp;
108 }
109 
110 
111 void buble_sort(float a[],const int size)
112 {
113  int i,j;
114  for(i=0; i<(size-1); i++)
115  {
116  for(j=0; j<(size-(i+1)); j++)
117  {
118  {
119  if(a[j] > a[j+1])
120  swap(&a[j],&a[j+1]);
121  }
122 
123  }
124  }
125 }
126 
uint8_t addToPayload(uint8_t b)
Definition: BayEOS.h:469
void startDataFrame(uint8_t subtype=BayEOS_Float32le, uint8_t checksum=0)
Definition: BayEOS.cpp:12
Definition: MCP342x.h:17
void setConf(uint8_t adc_addr, uint8_t conf)
Definition: MCP342x.cpp:91
float getData(byte adc_addr)
Definition: MCP342x.cpp:127