]> git.tdb.fi Git - model-railway-devices.git/commitdiff
Provide peak current since last read master
authorMikko Rasa <tdb@tdb.fi>
Tue, 1 Apr 2014 10:54:17 +0000 (13:54 +0300)
committerMikko Rasa <tdb@tdb.fi>
Tue, 1 Apr 2014 10:54:17 +0000 (13:54 +0300)
arducontrol/monitor.c

index c3804fc5c5632f37b3941e9feff83994105f31ac..99f0d4598d27f40f4cf05d156ddf89e5070c131c 100644 (file)
@@ -7,6 +7,7 @@
 static uint16_t track_current_samples[16] = { 0 };
 static uint8_t track_current_head = 0;
 static uint16_t track_current_sum = 0;
+static uint16_t track_current_peak = 0;
 static uint16_t overcurrent_limit = 9707;
 static uint8_t overcurrent_sent = 0;
 
@@ -17,6 +18,8 @@ static uint16_t input_voltage_sum = 0;
 static volatile uint8_t adc_state = 0;
 static volatile uint16_t adc_value = 0;
 
+static uint16_t current_milliamps(uint16_t);
+
 void monitor_init(void)
 {
        DDRB |= 0x02;
@@ -37,6 +40,9 @@ void monitor_check(void)
                        track_current_sum += value;
                        track_current_head = (i+1)&15;
 
+                       if(track_current_sum>track_current_peak)
+                               track_current_peak = track_current_sum;
+
                        if(track_current_sum>overcurrent_limit)
                        {
                                output_set_power(0);
@@ -75,10 +81,15 @@ uint8_t monitor_command(const uint8_t *cmd_buf, uint8_t cmd_length)
                        return LENGTH_ERROR;
 
                uint16_t value = monitor_track_current();
-               uint8_t reply[3];
+               uint16_t peak = current_milliamps(track_current_peak);
+               track_current_peak = 0;
+
+               uint8_t reply[5];
                reply[0] = TRACK_CURRENT;
                reply[1] = value>>8;
                reply[2] = value;
+               reply[3] = peak>>8;
+               reply[4] = peak;
                interface_send(reply, sizeof(reply));
        }
        else if(cmd_buf[0]==SET_OVERCURRENT_LIMIT)
@@ -113,10 +124,8 @@ uint8_t monitor_command(const uint8_t *cmd_buf, uint8_t cmd_length)
        return COMMAND_OK;
 }
 
-uint16_t monitor_track_current(void)
+static uint16_t current_milliamps(uint16_t value)
 {
-       uint16_t value = track_current_sum;
-
        // Convert to milliamps: (v/16*5/1024-2.5)*1000/0.185
        if(value<8192)  // Ignore negative current readings
                return 0;
@@ -130,6 +139,11 @@ uint16_t monitor_track_current(void)
        }
 }
 
+uint16_t monitor_track_current(void)
+{
+       return current_milliamps(track_current_sum);
+}
+
 uint16_t monitor_input_voltage(void)
 {
        uint16_t value = input_voltage_sum;