blob: 1cb61103ab2b4c9da2f8712d07d654a6e2f05603 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
  | 
/**
 * 
 * Author: Dylan Muller
 * Copyright (c) 2025 
 * All rights reserved.
 * 
 * - Commercial/IP use prohibited.
 * - Attribution required.
 * See License.txt
 *
 */
#include "setup.h"
#include "peripheral/timer.h"
#include "driver/mcp3202.h"
#include "driver/mcp4725.h"
#include "driver/si4468.h"
#include "module/gate.h"
#include "module/drain.h"
#include "util.h"
#include "status.h"
#include <stdlib.h>
#include <stdio.h>
#include <avr/interrupt.h>
#include <util/delay.h>
extern t_drain_status drain_status;
extern t_system_status system_status;
uint16_t drain_sweep_pwr(
    int16_t target_fw_pwr
)
{
    uint16_t drain_count = 0x0;
    uint16_t drain_inc = 0x0;
    uint16_t fw_pwr_adc = 0x0;
    int16_t fw_pwr = 0x0;
    uint16_t test_count = 0x0;
    int16_t test_boundary = DRAIN_TEST_BOUNDARY;
    system_status.status = SYSTEM_STATUS_BUSY;
    drain_status.status = DRAIN_STATUS_POWER_CONTROL;
    drain_count = dac_read(MCP4725_ID_DRAIN_VOLT, 1);
    _delay_ms(TRANSACT_DELAY_MS);
    si4468_tx_mode();
    _delay_ms(TRANSACT_DELAY_MS);
    dac_write(MCP4725_ID_DRAIN_VOLT, drain_count, 1);
    _delay_ms(TRANSACT_DELAY_MS);
    fw_pwr_adc = adc_read_n(MCP3202_ID_FW_POWER, 10);
    util_count_to_pwr(fw_pwr_adc, &fw_pwr);
    _delay_ms(TRANSACT_DELAY_MS);
    timer_reset();
    uint32_t time_start = timer_millis();
    bool timeout = false;
    while ((test_count < DRAIN_TEST_COUNT) && 
           (drain_count < DRAIN_PROBE_LIMIT))
    {
        if ((timer_millis() - time_start) > DRAIN_SETPOINT_TIMEOUT_MS)
        {
            timeout = true;
            break;
        }
        if (abs(fw_pwr - target_fw_pwr) > DRAIN_PROBE_INC_THRESHOLD)
        {
            drain_inc = DRAIN_PROBE_INC_COARSE;
        }
        else
        {
            drain_inc = DRAIN_PROBE_INC_FINE;
        }
            
        if (fw_pwr < (target_fw_pwr))
        {
            drain_count += drain_inc;
        }
        else if (fw_pwr > (target_fw_pwr))
        {
            drain_count -= drain_inc;
        }
        dac_write(MCP4725_ID_DRAIN_VOLT, drain_count, 1);
        _delay_us(DRAIN_LOOP_DELAY_US);
        fw_pwr_adc = adc_read_n(MCP3202_ID_FW_POWER, DRAIN_POWER_SAMPLES);
        util_count_to_pwr(fw_pwr_adc, &fw_pwr);
        if (fw_pwr <= (target_fw_pwr + test_boundary) &&
            fw_pwr >= (target_fw_pwr - test_boundary))
            {
                test_count += 1;
            }
    }
    system_status.d_time = (uint16_t)(timer_millis() - time_start);
    if (timeout)
    {
        system_status.status = SYSTEM_STATUS_SETPOINT_TIMEOUT;
    }
    else if(test_count >= DRAIN_TEST_COUNT)
    {
        system_status.status = SYSTEM_STATUS_SETPOINT_REACHED;
    }
    else
    {
        system_status.status = SYSTEM_STATUS_SETPOINT_FAIL;
    }
    return test_count;
}
  |