last

Dependencies:   Buffer INA2xx PID mbed

Committer:
jotaemesousa
Date:
Tue Oct 11 21:11:01 2022 +0000
Revision:
1:aaa70b16382f
Parent:
0:94b4516b2f32
last;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jotaemesousa 0:94b4516b2f32 1 #include "mbed.h"
jotaemesousa 0:94b4516b2f32 2 #include "ina2xx.h"
jotaemesousa 0:94b4516b2f32 3 #include "MyBuffer.h"
jotaemesousa 0:94b4516b2f32 4 #include "string.h"
jotaemesousa 0:94b4516b2f32 5 #include "mcp4725.h"
jotaemesousa 0:94b4516b2f32 6 #include "PID.h"
jotaemesousa 0:94b4516b2f32 7
jotaemesousa 0:94b4516b2f32 8 #define BUFFER_SIZE 50
jotaemesousa 0:94b4516b2f32 9 typedef enum {
jotaemesousa 0:94b4516b2f32 10 DC_LOAD_MANUAL = 0,
jotaemesousa 0:94b4516b2f32 11 DC_LOAD_AUTO_CURRENT,
jotaemesousa 0:94b4516b2f32 12 DC_LOAD_AUTO_POWER
jotaemesousa 0:94b4516b2f32 13 } DCLoadMode;
jotaemesousa 0:94b4516b2f32 14
jotaemesousa 0:94b4516b2f32 15 Ticker ticker_v, ticker_a, ticker_w, ticker_t;
jotaemesousa 0:94b4516b2f32 16
jotaemesousa 0:94b4516b2f32 17 Serial serial(PTB1,PTB2);
jotaemesousa 0:94b4516b2f32 18 I2C i2c(PTB4, PTB3);
jotaemesousa 0:94b4516b2f32 19 MyBuffer <char>buffer;
jotaemesousa 0:94b4516b2f32 20
jotaemesousa 0:94b4516b2f32 21 float voltage = 0.0, current = 0.0, power = 0.0;
jotaemesousa 0:94b4516b2f32 22 float volts_streaming = 0.0, amps_streaming = 0.0, watts_streaming = 0.0;
jotaemesousa 0:94b4516b2f32 23 float minvolts = 0.0;
jotaemesousa 0:94b4516b2f32 24 bool values_ok;
jotaemesousa 0:94b4516b2f32 25 DCLoadMode mode = DC_LOAD_MANUAL;
jotaemesousa 0:94b4516b2f32 26 uint8_t parse_buffer[BUFFER_SIZE+2];
jotaemesousa 0:94b4516b2f32 27 uint8_t i_ptr = 0;
jotaemesousa 0:94b4516b2f32 28 uint32_t timestamp = 0;
jotaemesousa 0:94b4516b2f32 29 bool timestamp_mode = false;
jotaemesousa 0:94b4516b2f32 30
jotaemesousa 0:94b4516b2f32 31 void ticker_v_func();
jotaemesousa 0:94b4516b2f32 32 void ticker_a_func();
jotaemesousa 0:94b4516b2f32 33 void ticker_w_func();
jotaemesousa 0:94b4516b2f32 34 void ticker_time();
jotaemesousa 0:94b4516b2f32 35
jotaemesousa 0:94b4516b2f32 36 void serialInt(void)
jotaemesousa 0:94b4516b2f32 37 {
jotaemesousa 0:94b4516b2f32 38 while(serial.readable()) {
jotaemesousa 0:94b4516b2f32 39 buffer.put(serial.getc());
jotaemesousa 0:94b4516b2f32 40 }
jotaemesousa 0:94b4516b2f32 41 }
jotaemesousa 0:94b4516b2f32 42
jotaemesousa 0:94b4516b2f32 43 void getVoltageCurrentPower()
jotaemesousa 0:94b4516b2f32 44 {
jotaemesousa 0:94b4516b2f32 45 bool ret, ret2;
jotaemesousa 0:94b4516b2f32 46 voltage = ina226GetVoltage(&i2c, &ret);
jotaemesousa 0:94b4516b2f32 47 current = ina226GetCurrent(&i2c, &ret2);
jotaemesousa 0:94b4516b2f32 48 values_ok = ret * ret2;
jotaemesousa 0:94b4516b2f32 49 power = voltage * current;
jotaemesousa 0:94b4516b2f32 50 }
jotaemesousa 0:94b4516b2f32 51
jotaemesousa 0:94b4516b2f32 52 void parseCmd()
jotaemesousa 0:94b4516b2f32 53 {
jotaemesousa 0:94b4516b2f32 54 while(buffer.available() && i_ptr < BUFFER_SIZE) {
jotaemesousa 0:94b4516b2f32 55 parse_buffer[i_ptr] = buffer.get();
jotaemesousa 0:94b4516b2f32 56 serial.putc(parse_buffer[i_ptr++]);
jotaemesousa 0:94b4516b2f32 57 parse_buffer[i_ptr] = 0;
jotaemesousa 0:94b4516b2f32 58 char * endstr = NULL;
jotaemesousa 0:94b4516b2f32 59 endstr = strstr((char *)parse_buffer, "\r\n");
jotaemesousa 0:94b4516b2f32 60 if(endstr != NULL) {
jotaemesousa 0:94b4516b2f32 61 i_ptr = 0;
jotaemesousa 0:94b4516b2f32 62 char *pch = strstr((char *)parse_buffer,"=");
jotaemesousa 0:94b4516b2f32 63 char *pch2 = strstr((char *)parse_buffer,"?");
jotaemesousa 0:94b4516b2f32 64 int value = 0;
jotaemesousa 0:94b4516b2f32 65 if(pch != NULL) {
jotaemesousa 0:94b4516b2f32 66 if(!strncmp((char *)parse_buffer, "SV", 2)) {
jotaemesousa 0:94b4516b2f32 67 value = atoi(pch+1); // voltage
jotaemesousa 0:94b4516b2f32 68 if(value <= 50) {
jotaemesousa 0:94b4516b2f32 69 volts_streaming = 0.0;
jotaemesousa 0:94b4516b2f32 70 ticker_v.detach();
jotaemesousa 0:94b4516b2f32 71 } else {
jotaemesousa 0:94b4516b2f32 72
jotaemesousa 0:94b4516b2f32 73 ticker_v.detach();
jotaemesousa 0:94b4516b2f32 74 volts_streaming = (float)value/1000.0;
jotaemesousa 0:94b4516b2f32 75 ticker_v.attach(&ticker_v_func, volts_streaming);
jotaemesousa 0:94b4516b2f32 76 }
jotaemesousa 0:94b4516b2f32 77 } else if(!strncmp((char *)parse_buffer, "SC", 2)) {
jotaemesousa 0:94b4516b2f32 78 value = atoi(pch+1); // amps
jotaemesousa 0:94b4516b2f32 79 if(value <= 50) {
jotaemesousa 0:94b4516b2f32 80 amps_streaming = 0.0;
jotaemesousa 0:94b4516b2f32 81 ticker_a.detach();
jotaemesousa 0:94b4516b2f32 82 } else {
jotaemesousa 0:94b4516b2f32 83
jotaemesousa 0:94b4516b2f32 84 ticker_a.detach();
jotaemesousa 0:94b4516b2f32 85 amps_streaming = (float)value/1000.0;
jotaemesousa 0:94b4516b2f32 86 ticker_a.attach(&ticker_a_func, amps_streaming);
jotaemesousa 0:94b4516b2f32 87 }
jotaemesousa 0:94b4516b2f32 88 } else if(!strncmp((char *)parse_buffer, "SP", 2)) {
jotaemesousa 0:94b4516b2f32 89 value = atoi(pch+1); // watts_streaming
jotaemesousa 0:94b4516b2f32 90 if(value <= 50) {
jotaemesousa 0:94b4516b2f32 91 watts_streaming = 0.0;
jotaemesousa 0:94b4516b2f32 92 ticker_w.detach();
jotaemesousa 0:94b4516b2f32 93 } else {
jotaemesousa 0:94b4516b2f32 94
jotaemesousa 0:94b4516b2f32 95 ticker_w.detach();
jotaemesousa 0:94b4516b2f32 96 watts_streaming = (float)value/1000.0;
jotaemesousa 0:94b4516b2f32 97 ticker_w.attach(&ticker_w_func, watts_streaming);
jotaemesousa 0:94b4516b2f32 98 }
jotaemesousa 0:94b4516b2f32 99 } else if(!strncmp((char *)parse_buffer, "A", 1)) {
jotaemesousa 0:94b4516b2f32 100 value = atoi(pch+1); // set current
jotaemesousa 0:94b4516b2f32 101 float value_real = value;
jotaemesousa 0:94b4516b2f32 102 value_real *= 0.1866;
jotaemesousa 0:94b4516b2f32 103 mcp4725_setVoltage(&i2c, (int)value_real, false);
jotaemesousa 0:94b4516b2f32 104 } else if(!strncmp((char *)parse_buffer, "Tm", 2)) {
jotaemesousa 0:94b4516b2f32 105 timestamp_mode = atoi(pch+1)?true:false; // time
jotaemesousa 0:94b4516b2f32 106 } else if(!strncmp((char *)parse_buffer, "T", 1)) {
jotaemesousa 0:94b4516b2f32 107 timestamp = atoi(pch+1); // time
jotaemesousa 0:94b4516b2f32 108 } else if(!strncmp((char *)parse_buffer, "mV", 2)) {
jotaemesousa 0:94b4516b2f32 109 minvolts = (float)(atoi(pch+1))/1000.0; // time
jotaemesousa 0:94b4516b2f32 110 }
jotaemesousa 0:94b4516b2f32 111 } else if(pch2 != NULL) {
jotaemesousa 0:94b4516b2f32 112 if(!strncmp((char *)parse_buffer, "A?", 2)) {
jotaemesousa 0:94b4516b2f32 113 printf("C=%d\r\n", (int)(current * 1000.0));
jotaemesousa 0:94b4516b2f32 114 } else if(!strncmp((char *)parse_buffer, "V?", 2)) {
jotaemesousa 0:94b4516b2f32 115 printf("V=%d\r\n", (int)(voltage * 1000.0));
jotaemesousa 0:94b4516b2f32 116 } else if(!strncmp((char *)parse_buffer, "P?", 2)) {
jotaemesousa 0:94b4516b2f32 117 printf("P=%d\r\n", (int)(power * 1000.0));
jotaemesousa 0:94b4516b2f32 118 } else if(!strncmp((char *)parse_buffer, "T?", 2)) {
jotaemesousa 0:94b4516b2f32 119 printf("T=%d\r\n", timestamp);
jotaemesousa 0:94b4516b2f32 120 }
jotaemesousa 0:94b4516b2f32 121 } else {
jotaemesousa 0:94b4516b2f32 122 if(!strncmp((char *)parse_buffer, "help", 4)) {
jotaemesousa 0:94b4516b2f32 123 serial.puts("Available commands:\r\nSV=<ms>\\r\\n - Set streaming voltage\r\n");
jotaemesousa 0:94b4516b2f32 124 serial.puts("SC=<ms>\\r\\n - Set streaming current\r\n");
jotaemesousa 0:94b4516b2f32 125 serial.puts("SP=<ms>\\r\\n - Set streaming power\r\n");
jotaemesousa 0:94b4516b2f32 126 serial.puts("A=<mA>\\r\\n - Set discharge current\r\n");
jotaemesousa 0:94b4516b2f32 127 serial.puts("T=<ms>\\r\\n - Set timestamp\r\n");
jotaemesousa 0:94b4516b2f32 128 serial.puts("Tm=<ms>\\r\\n - Set timestamp mode (0: timestamp off, 1: timestamp on)\r\n");
jotaemesousa 0:94b4516b2f32 129 serial.puts("mV=<mV>\\r\\n - Set minimum discharge voltage\r\n");
jotaemesousa 0:94b4516b2f32 130 serial.puts("V?\\r\\n - Get voltage\r\n");
jotaemesousa 0:94b4516b2f32 131 serial.puts("A?\\r\\n - Get discharge current\r\n");
jotaemesousa 0:94b4516b2f32 132 serial.puts("P?\\r\\n - Get discharge power\r\n");
jotaemesousa 0:94b4516b2f32 133 serial.puts("T?\\r\\n - Get timestamp\r\n");
jotaemesousa 0:94b4516b2f32 134 serial.puts("help\\r\\n - help menu\r\n");
jotaemesousa 0:94b4516b2f32 135 serial.puts("report\\r\\n - Get sensors report\r\n");
jotaemesousa 0:94b4516b2f32 136 } else if(!strncmp((char *)parse_buffer, "report", 6)) {
jotaemesousa 0:94b4516b2f32 137 serial.puts("DC-LOAD report\r\n");
jotaemesousa 0:94b4516b2f32 138
jotaemesousa 0:94b4516b2f32 139 } else {
jotaemesousa 0:94b4516b2f32 140 serial.puts("Invalid command...\nSend <help\\r\\n> to see available commands\n");
jotaemesousa 0:94b4516b2f32 141 }
jotaemesousa 0:94b4516b2f32 142 }
jotaemesousa 0:94b4516b2f32 143 }
jotaemesousa 0:94b4516b2f32 144 }
jotaemesousa 0:94b4516b2f32 145 }
jotaemesousa 0:94b4516b2f32 146
jotaemesousa 0:94b4516b2f32 147 int main()
jotaemesousa 0:94b4516b2f32 148 {
jotaemesousa 0:94b4516b2f32 149 ticker_t.attach(&ticker_time, 0.01);
jotaemesousa 0:94b4516b2f32 150 serial.baud(115200);
jotaemesousa 0:94b4516b2f32 151 serial.puts("DC-LOAD starting...\nSend <help\\r\\n> to see available commands\n");
jotaemesousa 0:94b4516b2f32 152 serial.attach(serialInt);
jotaemesousa 0:94b4516b2f32 153 i2c.frequency(100000);
jotaemesousa 0:94b4516b2f32 154 i_ptr = 0;
jotaemesousa 0:94b4516b2f32 155 uint16_t conf = 0;
jotaemesousa 0:94b4516b2f32 156 mcp4725_setVoltage(&i2c, 0, false);
jotaemesousa 0:94b4516b2f32 157 ina226_set(&i2c, 0x00, 0x4927);
jotaemesousa 0:94b4516b2f32 158 ina226_get(&i2c, 0x00, &conf);
jotaemesousa 0:94b4516b2f32 159 if(conf != 0x4927) return 1;
jotaemesousa 0:94b4516b2f32 160 ina226_set(&i2c,0x05, 1000);
jotaemesousa 0:94b4516b2f32 161 ina226_get(&i2c, 0x05, &conf);
jotaemesousa 0:94b4516b2f32 162 if(conf != 1000) return 2;
jotaemesousa 0:94b4516b2f32 163 values_ok = false;
jotaemesousa 0:94b4516b2f32 164 mode = DC_LOAD_MANUAL;
jotaemesousa 0:94b4516b2f32 165
jotaemesousa 0:94b4516b2f32 166 while(1) {
jotaemesousa 0:94b4516b2f32 167 getVoltageCurrentPower();
jotaemesousa 0:94b4516b2f32 168
jotaemesousa 0:94b4516b2f32 169 parseCmd();
jotaemesousa 0:94b4516b2f32 170
jotaemesousa 0:94b4516b2f32 171 if( minvolts > 1.0 && voltage < minvolts) {
jotaemesousa 0:94b4516b2f32 172 printf("Min. Voltage detected\r\n");
jotaemesousa 0:94b4516b2f32 173 mcp4725_setVoltage(&i2c, 0x0000, false);
jotaemesousa 0:94b4516b2f32 174 }
jotaemesousa 0:94b4516b2f32 175 wait(0.1);
jotaemesousa 0:94b4516b2f32 176 }
jotaemesousa 0:94b4516b2f32 177 }
jotaemesousa 0:94b4516b2f32 178
jotaemesousa 0:94b4516b2f32 179 void ticker_v_func(void)
jotaemesousa 0:94b4516b2f32 180 {
jotaemesousa 0:94b4516b2f32 181 if(timestamp_mode) {
jotaemesousa 0:94b4516b2f32 182 printf("%d:V=%d\r\n", timestamp, (int)(voltage * 1000.0));
jotaemesousa 0:94b4516b2f32 183 } else {
jotaemesousa 0:94b4516b2f32 184 printf("V=%d\r\n", (int)(voltage * 1000.0));
jotaemesousa 0:94b4516b2f32 185 }
jotaemesousa 0:94b4516b2f32 186 }
jotaemesousa 0:94b4516b2f32 187
jotaemesousa 0:94b4516b2f32 188 void ticker_a_func(void)
jotaemesousa 0:94b4516b2f32 189 {
jotaemesousa 0:94b4516b2f32 190 if(timestamp_mode) {
jotaemesousa 0:94b4516b2f32 191 printf("%d:C=%d\r\n", timestamp, (int)(current * 1000.0));
jotaemesousa 0:94b4516b2f32 192 } else {
jotaemesousa 0:94b4516b2f32 193 printf("C=%d\r\n", (int)(current * 1000.0));
jotaemesousa 0:94b4516b2f32 194 }
jotaemesousa 0:94b4516b2f32 195 }
jotaemesousa 0:94b4516b2f32 196
jotaemesousa 0:94b4516b2f32 197 void ticker_w_func(void)
jotaemesousa 0:94b4516b2f32 198 {
jotaemesousa 0:94b4516b2f32 199 if(timestamp_mode) {
jotaemesousa 0:94b4516b2f32 200 printf("%d:P=%d\r\n", timestamp, (int)(power * 1000.0));
jotaemesousa 0:94b4516b2f32 201 } else {
jotaemesousa 0:94b4516b2f32 202 printf("P=%d\r\n", (int)(power * 1000.0));
jotaemesousa 0:94b4516b2f32 203 }
jotaemesousa 0:94b4516b2f32 204 }
jotaemesousa 0:94b4516b2f32 205
jotaemesousa 0:94b4516b2f32 206 void ticker_time(void)
jotaemesousa 0:94b4516b2f32 207 {
jotaemesousa 0:94b4516b2f32 208 timestamp+=10;
jotaemesousa 0:94b4516b2f32 209 }