123
Fork of LG by
DeviceISACS.c@177:672ef279c8e0, 2016-07-03 (annotated)
- Committer:
- Diletant
- Date:
- Sun Jul 03 13:40:48 2016 +0000
- Revision:
- 177:672ef279c8e0
- Parent:
- 173:7f938afb0447
- Child:
- 183:c7a9c309086c
Device&... update. Some Ask_Gld functionality. Not final!!!
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Diletant | 161:efd949e8d536 | 1 | #include "Device.h" |
Diletant | 161:efd949e8d536 | 2 | |
Diletant | 161:efd949e8d536 | 3 | extern Device device; |
Diletant | 161:efd949e8d536 | 4 | |
Diletant | 167:bedc0a9d559a | 5 | void InitISACSOutputDefaultSettings(void){ |
Diletant | 177:672ef279c8e0 | 6 | device.isacs.regulator.settings.start.enabled = 1; |
Diletant | 177:672ef279c8e0 | 7 | device.isacs.regulator.settings.regular.enabled = 1; |
Diletant | 177:672ef279c8e0 | 8 | device.isacs.regulator.settings.reset.enabled = 1; |
Diletant | 177:672ef279c8e0 | 9 | |
Diletant | 177:672ef279c8e0 | 10 | device.isacs.regulator.settings.transfer.points = 16; |
Diletant | 177:672ef279c8e0 | 11 | device.isacs.regulator.settings.transfer.error[0] = -0x20000; //-2.0V |
Diletant | 177:672ef279c8e0 | 12 | device.isacs.regulator.settings.transfer.correction[0] = 0x10000; //1.0 |
Diletant | 177:672ef279c8e0 | 13 | device.isacs.regulator.settings.transfer.error[1] = -0x1bbbb; //-1.7333333333333334V |
Diletant | 177:672ef279c8e0 | 14 | device.isacs.regulator.settings.transfer.correction[1] = 0xb473; //0.7048888888888889 |
Diletant | 177:672ef279c8e0 | 15 | device.isacs.regulator.settings.transfer.error[2] = -0x17777; //-1.4666666666666668V |
Diletant | 177:672ef279c8e0 | 16 | device.isacs.regulator.settings.transfer.correction[2] = 0x7aa7; //0.47911111111111115 |
Diletant | 177:672ef279c8e0 | 17 | device.isacs.regulator.settings.transfer.error[3] = -0x13333; //-1.2V |
Diletant | 177:672ef279c8e0 | 18 | device.isacs.regulator.settings.transfer.correction[3] = 0x4fdf; //0.31199999999999994 |
Diletant | 177:672ef279c8e0 | 19 | device.isacs.regulator.settings.transfer.error[4] = -0xeeee; //-0.9333333333333333V |
Diletant | 177:672ef279c8e0 | 20 | device.isacs.regulator.settings.transfer.correction[4] = 0x3161; //0.1928888888888889 |
Diletant | 177:672ef279c8e0 | 21 | device.isacs.regulator.settings.transfer.error[5] = -0xaaaa; //-0.6666666666666667V |
Diletant | 177:672ef279c8e0 | 22 | device.isacs.regulator.settings.transfer.correction[5] = 0x1c71; //0.11111111111111113 |
Diletant | 177:672ef279c8e0 | 23 | device.isacs.regulator.settings.transfer.error[6] = -0x6666; //-0.3999999999999999V |
Diletant | 177:672ef279c8e0 | 24 | device.isacs.regulator.settings.transfer.correction[6] = 0xe56; //0.05599999999999999 |
Diletant | 177:672ef279c8e0 | 25 | device.isacs.regulator.settings.transfer.error[7] = -0x2222; //-0.1333333333333333V |
Diletant | 177:672ef279c8e0 | 26 | device.isacs.regulator.settings.transfer.correction[7] = 0x452; //0.016888888888888884 |
Diletant | 177:672ef279c8e0 | 27 | device.isacs.regulator.settings.transfer.error[8] = 0x2222; //0.1333333333333333V |
Diletant | 177:672ef279c8e0 | 28 | device.isacs.regulator.settings.transfer.correction[8] = -0x452; //-0.016888888888888884 |
Diletant | 177:672ef279c8e0 | 29 | device.isacs.regulator.settings.transfer.error[9] = 0x6666; //0.3999999999999999V |
Diletant | 177:672ef279c8e0 | 30 | device.isacs.regulator.settings.transfer.correction[9] = -0xe56; //-0.05599999999999999 |
Diletant | 177:672ef279c8e0 | 31 | device.isacs.regulator.settings.transfer.error[10] = 0xaaaa; //0.6666666666666665V |
Diletant | 177:672ef279c8e0 | 32 | device.isacs.regulator.settings.transfer.correction[10] = -0x1c71; //-0.11111111111111108 |
Diletant | 177:672ef279c8e0 | 33 | device.isacs.regulator.settings.transfer.error[11] = 0xeeee; //0.9333333333333331V |
Diletant | 177:672ef279c8e0 | 34 | device.isacs.regulator.settings.transfer.correction[11] = -0x3161; //-0.1928888888888888 |
Diletant | 177:672ef279c8e0 | 35 | device.isacs.regulator.settings.transfer.error[12] = 0x13333; //1.2000000000000002V |
Diletant | 177:672ef279c8e0 | 36 | device.isacs.regulator.settings.transfer.correction[12] = -0x4fdf; //-0.31200000000000006 |
Diletant | 177:672ef279c8e0 | 37 | device.isacs.regulator.settings.transfer.error[13] = 0x17777; //1.4666666666666668V |
Diletant | 177:672ef279c8e0 | 38 | device.isacs.regulator.settings.transfer.correction[13] = -0x7aa7; //-0.47911111111111115 |
Diletant | 177:672ef279c8e0 | 39 | device.isacs.regulator.settings.transfer.error[14] = 0x1bbbb; //1.7333333333333334V |
Diletant | 177:672ef279c8e0 | 40 | device.isacs.regulator.settings.transfer.correction[14] = -0xb473; //-0.7048888888888889 |
Diletant | 177:672ef279c8e0 | 41 | device.isacs.regulator.settings.transfer.error[15] = 0x20000; //2.0V |
Diletant | 177:672ef279c8e0 | 42 | device.isacs.regulator.settings.transfer.correction[15] = -0x10000; //-1.0 |
Diletant | 177:672ef279c8e0 | 43 | |
Diletant | 167:bedc0a9d559a | 44 | device.isacs.output.settings.transfer.points = 2; |
Diletant | 166:c3c0b8a90d81 | 45 | //Control point 1.5 volt - low output limit |
Diletant | 167:bedc0a9d559a | 46 | device.isacs.output.settings.transfer.voltage[0] = 0x00017fff; |
Diletant | 167:bedc0a9d559a | 47 | device.isacs.output.settings.transfer.code[0] = 800; |
Diletant | 166:c3c0b8a90d81 | 48 | //Control point 12.5 volt - high output limit |
Diletant | 167:bedc0a9d559a | 49 | device.isacs.output.settings.transfer.voltage[1] = 0x000c7fff; |
Diletant | 167:bedc0a9d559a | 50 | device.isacs.output.settings.transfer.code[1] = 2048; |
Diletant | 167:bedc0a9d559a | 51 | device.isacs.output.settings.start.voltage = 0x000c7fff; //+12.5V start |
Diletant | 167:bedc0a9d559a | 52 | device.isacs.output.settings.reset.voltage = 0x00077fff; //+7.5V if regulator disabled at reset time |
Diletant | 161:efd949e8d536 | 53 | } |
Diletant | 161:efd949e8d536 | 54 | |
Diletant | 167:bedc0a9d559a | 55 | void InitISACSOutputState(void){ |
Diletant | 161:efd949e8d536 | 56 | } |
Diletant | 161:efd949e8d536 | 57 | |
Diletant | 167:bedc0a9d559a | 58 | void DeviceStartISACSOutput(void){ |
Diletant | 167:bedc0a9d559a | 59 | } |
Diletant | 167:bedc0a9d559a | 60 | |
Diletant | 167:bedc0a9d559a | 61 | void InitISACSRegulatorDefaultSettings(void){ |
Diletant | 161:efd949e8d536 | 62 | //Startup settings |
Diletant | 166:c3c0b8a90d81 | 63 | device.isacs.regulator.settings.start.enabled = 1; |
Diletant | 161:efd949e8d536 | 64 | device.isacs.regulator.settings.start.reference = 0x00020000; //Reference voltage 2V in 16.16 signed fixed point format |
Diletant | 161:efd949e8d536 | 65 | device.isacs.regulator.settings.start.scale = 1; //Scale factor |
Diletant | 161:efd949e8d536 | 66 | //Regular operation settings |
Diletant | 166:c3c0b8a90d81 | 67 | device.isacs.regulator.settings.regular.enabled = 1; |
Diletant | 161:efd949e8d536 | 68 | device.isacs.regulator.settings.regular.reference = 0x00020000; //Reference voltage 2V in 16.16 signed fixed point format |
Diletant | 161:efd949e8d536 | 69 | device.isacs.regulator.settings.regular.scale = 1; //Scale factor |
Diletant | 161:efd949e8d536 | 70 | //Reset operation settings |
Diletant | 166:c3c0b8a90d81 | 71 | device.isacs.regulator.settings.reset.enabled = 1; |
Diletant | 161:efd949e8d536 | 72 | device.isacs.regulator.settings.reset.reference = 0x00020000; //Reference voltage 2V in 16.16 signed fixed point format |
Diletant | 161:efd949e8d536 | 73 | device.isacs.regulator.settings.reset.scale = 1; //Scale factor |
Diletant | 161:efd949e8d536 | 74 | } |
Diletant | 161:efd949e8d536 | 75 | |
Diletant | 167:bedc0a9d559a | 76 | void InitISACSRegulatorState(void){ |
Diletant | 166:c3c0b8a90d81 | 77 | device.isacs.regulator.state.enabled = device.isacs.regulator.settings.start.enabled; |
Diletant | 161:efd949e8d536 | 78 | device.isacs.regulator.state.reference = device.isacs.regulator.settings.start.reference; |
Diletant | 161:efd949e8d536 | 79 | device.isacs.regulator.state.scale = device.isacs.regulator.settings.start.scale; |
Diletant | 177:672ef279c8e0 | 80 | device.isacs.regulator.state.error = 0; |
Diletant | 177:672ef279c8e0 | 81 | device.isacs.regulator.state.correction = 0; |
Diletant | 161:efd949e8d536 | 82 | } |
Diletant | 161:efd949e8d536 | 83 | |
Diletant | 167:bedc0a9d559a | 84 | void DeviceStartISACSRegulator(void){ |
Diletant | 167:bedc0a9d559a | 85 | } |
Diletant | 167:bedc0a9d559a | 86 | |
Diletant | 167:bedc0a9d559a | 87 | void InitISACSDefaultSettings(void){ |
Diletant | 167:bedc0a9d559a | 88 | InitISACSPotentiometersDefaultSettings(); |
Diletant | 167:bedc0a9d559a | 89 | InitISACSRegulatorDefaultSettings(); |
Diletant | 167:bedc0a9d559a | 90 | InitISACSOutputDefaultSettings(); |
Diletant | 161:efd949e8d536 | 91 | } |
Diletant | 161:efd949e8d536 | 92 | |
Diletant | 167:bedc0a9d559a | 93 | void InitISACSState(void){ |
Diletant | 167:bedc0a9d559a | 94 | InitISACSPotentiometersState(); |
Diletant | 167:bedc0a9d559a | 95 | InitISACSRegulatorState(); |
Diletant | 167:bedc0a9d559a | 96 | InitISACSOutputState(); |
Diletant | 161:efd949e8d536 | 97 | } |
Diletant | 161:efd949e8d536 | 98 | |
Diletant | 167:bedc0a9d559a | 99 | void DeviceStartISACS(void){ |
Diletant | 167:bedc0a9d559a | 100 | DeviceStartISACSPotentiometers(); |
Diletant | 167:bedc0a9d559a | 101 | DeviceStartISACSRegulator(); |
Diletant | 167:bedc0a9d559a | 102 | DeviceStartISACSOutput(); |
Diletant | 161:efd949e8d536 | 103 | } |
Diletant | 161:efd949e8d536 | 104 | |
Diletant | 166:c3c0b8a90d81 | 105 | /* |
Diletant | 166:c3c0b8a90d81 | 106 | //Count leading zeroes |
Diletant | 166:c3c0b8a90d81 | 107 | static uint8_t clz(uint32_t x) { |
Diletant | 166:c3c0b8a90d81 | 108 | uint8_t result = 0; |
Diletant | 166:c3c0b8a90d81 | 109 | if (x == 0) return 32; |
Diletant | 166:c3c0b8a90d81 | 110 | while (!(x & 0xF0000000)) { result += 4; x <<= 4; } |
Diletant | 166:c3c0b8a90d81 | 111 | while (!(x & 0x80000000)) { result += 1; x <<= 1; } |
Diletant | 166:c3c0b8a90d81 | 112 | return result; |
Diletant | 166:c3c0b8a90d81 | 113 | } |
Diletant | 166:c3c0b8a90d81 | 114 | //Count trailing zeroes |
Diletant | 166:c3c0b8a90d81 | 115 | static uint8_t ctz(uint32_t x) { |
Diletant | 166:c3c0b8a90d81 | 116 | uint8_t result = 0; |
Diletant | 166:c3c0b8a90d81 | 117 | if (x == 0) return 32; |
Diletant | 166:c3c0b8a90d81 | 118 | while (!(x & 0xF0000000)) { result += 4; x <<= 4; } |
Diletant | 166:c3c0b8a90d81 | 119 | while (!(x & 0x80000000)) { result += 1; x <<= 1; } |
Diletant | 166:c3c0b8a90d81 | 120 | return result; |
Diletant | 166:c3c0b8a90d81 | 121 | } |
Diletant | 166:c3c0b8a90d81 | 122 | */ |
Diletant | 166:c3c0b8a90d81 | 123 | |
Diletant | 166:c3c0b8a90d81 | 124 | int32_t isacsInterpolate(int32_t a,int32_t a1,int32_t a2,int32_t b1,int32_t b2) { |
Diletant | 166:c3c0b8a90d81 | 125 | int32_t ma, mb; |
Diletant | 166:c3c0b8a90d81 | 126 | while (1) { |
Diletant | 166:c3c0b8a90d81 | 127 | if (a1 == a) return b1; |
Diletant | 166:c3c0b8a90d81 | 128 | if (a2 == a) return b2; |
Diletant | 166:c3c0b8a90d81 | 129 | |
Diletant | 166:c3c0b8a90d81 | 130 | ma = (a1 + a2) >> 1; |
Diletant | 166:c3c0b8a90d81 | 131 | mb = (b1 + b2) >> 1; |
Diletant | 166:c3c0b8a90d81 | 132 | |
Diletant | 166:c3c0b8a90d81 | 133 | if (a < ma) { |
Diletant | 166:c3c0b8a90d81 | 134 | if (a2 == ma) return mb; |
Diletant | 166:c3c0b8a90d81 | 135 | if (b1 == mb) return mb; |
Diletant | 166:c3c0b8a90d81 | 136 | a2 = ma; b2 = mb; |
Diletant | 166:c3c0b8a90d81 | 137 | } else if (a > ma) { |
Diletant | 166:c3c0b8a90d81 | 138 | if (a1 == ma) return mb; |
Diletant | 166:c3c0b8a90d81 | 139 | if (b2 == mb) return mb; |
Diletant | 166:c3c0b8a90d81 | 140 | a1 = ma; b1 = mb; |
Diletant | 166:c3c0b8a90d81 | 141 | } else return mb; |
Diletant | 166:c3c0b8a90d81 | 142 | } |
Diletant | 166:c3c0b8a90d81 | 143 | } |
Diletant | 166:c3c0b8a90d81 | 144 | |
Diletant | 177:672ef279c8e0 | 145 | int32_t isacsInput(uint32_t raw) { |
Diletant | 177:672ef279c8e0 | 146 | int32_t r1, r2; |
Diletant | 167:bedc0a9d559a | 147 | for (uint8_t i = 1; i < device.isacs.input.settings.transfer.points; i++) { |
Diletant | 177:672ef279c8e0 | 148 | r1 = device.isacs.input.settings.transfer.raw[i - 1]; |
Diletant | 177:672ef279c8e0 | 149 | if (raw < r1) { |
Diletant | 177:672ef279c8e0 | 150 | return device.isacs.input.settings.transfer.V[i - 1]; |
Diletant | 166:c3c0b8a90d81 | 151 | } |
Diletant | 166:c3c0b8a90d81 | 152 | |
Diletant | 177:672ef279c8e0 | 153 | r2 = device.isacs.input.settings.transfer.raw[i]; |
Diletant | 177:672ef279c8e0 | 154 | if (raw < r2) { |
Diletant | 177:672ef279c8e0 | 155 | int32_t v1 = device.isacs.input.settings.transfer.V[i - 1]; |
Diletant | 177:672ef279c8e0 | 156 | int32_t v2 = device.isacs.input.settings.transfer.V[i]; |
Diletant | 177:672ef279c8e0 | 157 | return isacsInterpolate(raw, r1, r2, v1, v2); |
Diletant | 166:c3c0b8a90d81 | 158 | } |
Diletant | 166:c3c0b8a90d81 | 159 | } |
Diletant | 177:672ef279c8e0 | 160 | return device.isacs.input.settings.transfer.V[device.isacs.input.settings.transfer.points - 1]; |
Diletant | 177:672ef279c8e0 | 161 | } |
Diletant | 177:672ef279c8e0 | 162 | |
Diletant | 177:672ef279c8e0 | 163 | uint32_t isacsCorrection(int32_t error) { |
Diletant | 177:672ef279c8e0 | 164 | int32_t e1, e2; |
Diletant | 177:672ef279c8e0 | 165 | for (uint8_t i = 1; i < device.isacs.regulator.settings.transfer.points; i++) { |
Diletant | 177:672ef279c8e0 | 166 | e1 = device.isacs.regulator.settings.transfer.error[i - 1]; |
Diletant | 177:672ef279c8e0 | 167 | if (error < e1) { |
Diletant | 177:672ef279c8e0 | 168 | return device.isacs.regulator.settings.transfer.correction[i - 1]; |
Diletant | 177:672ef279c8e0 | 169 | } |
Diletant | 177:672ef279c8e0 | 170 | |
Diletant | 177:672ef279c8e0 | 171 | e2 = device.isacs.regulator.settings.transfer.error[i]; |
Diletant | 177:672ef279c8e0 | 172 | if (error < e2) { |
Diletant | 177:672ef279c8e0 | 173 | int32_t c1 = device.isacs.regulator.settings.transfer.correction[i - 1]; |
Diletant | 177:672ef279c8e0 | 174 | int32_t c2 = device.isacs.regulator.settings.transfer.correction[i]; |
Diletant | 177:672ef279c8e0 | 175 | return isacsInterpolate(error, e1, e2, c1, c2); |
Diletant | 177:672ef279c8e0 | 176 | } |
Diletant | 177:672ef279c8e0 | 177 | } |
Diletant | 177:672ef279c8e0 | 178 | return device.isacs.regulator.settings.transfer.correction[device.isacs.regulator.settings.transfer.points - 1]; |
Diletant | 166:c3c0b8a90d81 | 179 | } |
Diletant | 166:c3c0b8a90d81 | 180 | |
Diletant | 173:7f938afb0447 | 181 | int32_t isacsOutput(uint32_t voltage) { |
Diletant | 166:c3c0b8a90d81 | 182 | int32_t v1, v2; |
Diletant | 167:bedc0a9d559a | 183 | for (uint8_t i = 1; i < device.isacs.output.settings.transfer.points; i++) { |
Diletant | 167:bedc0a9d559a | 184 | v1 = device.isacs.output.settings.transfer.voltage[i - 1]; |
Diletant | 166:c3c0b8a90d81 | 185 | if (voltage < v1) { |
Diletant | 167:bedc0a9d559a | 186 | return device.isacs.output.settings.transfer.code[i - 1]; |
Diletant | 166:c3c0b8a90d81 | 187 | } |
Diletant | 166:c3c0b8a90d81 | 188 | |
Diletant | 167:bedc0a9d559a | 189 | v2 = device.isacs.output.settings.transfer.voltage[i]; |
Diletant | 166:c3c0b8a90d81 | 190 | if (voltage < v2) { |
Diletant | 167:bedc0a9d559a | 191 | int32_t c1 = device.isacs.output.settings.transfer.code[i - 1]; |
Diletant | 167:bedc0a9d559a | 192 | int32_t c2 = device.isacs.output.settings.transfer.code[i]; |
Diletant | 166:c3c0b8a90d81 | 193 | return isacsInterpolate(voltage, v1, v2, c1, c2); |
Diletant | 166:c3c0b8a90d81 | 194 | } |
Diletant | 166:c3c0b8a90d81 | 195 | } |
Diletant | 167:bedc0a9d559a | 196 | return device.isacs.output.settings.transfer.code[device.isacs.output.settings.transfer.points - 1]; |
Diletant | 166:c3c0b8a90d81 | 197 | } |
Diletant | 166:c3c0b8a90d81 | 198 | |
Diletant | 166:c3c0b8a90d81 | 199 | void isacsProcess(void) { |
Diletant | 177:672ef279c8e0 | 200 | device.isacs.input.state.sum += device.controller.SSP.in[4]; |
Diletant | 166:c3c0b8a90d81 | 201 | if (device.measurement.counter == 31) { |
Diletant | 177:672ef279c8e0 | 202 | device.isacs.input.state.raw = device.isacs.input.state.sum >> 5; |
Diletant | 167:bedc0a9d559a | 203 | device.isacs.input.state.sum = 0; |
Diletant | 177:672ef279c8e0 | 204 | device.isacs.input.state.V = isacsInput(device.isacs.input.state.raw); |
Diletant | 166:c3c0b8a90d81 | 205 | |
Diletant | 166:c3c0b8a90d81 | 206 | if (device.isacs.regulator.state.enabled) { |
Diletant | 177:672ef279c8e0 | 207 | device.isacs.regulator.state.error = device.isacs.regulator.state.reference - device.isacs.input.state.V; |
Diletant | 177:672ef279c8e0 | 208 | device.isacs.regulator.state.correction = (device.isacs.regulator.state.scale * isacsCorrection(device.isacs.regulator.state.error)) >> 16; |
Diletant | 177:672ef279c8e0 | 209 | device.isacs.output.state.voltage += device.isacs.regulator.state.correction; |
Diletant | 173:7f938afb0447 | 210 | device.controller.SSP.out[0] = isacsOutput(device.isacs.output.state.voltage); |
Diletant | 166:c3c0b8a90d81 | 211 | } |
Diletant | 166:c3c0b8a90d81 | 212 | } |
Diletant | 161:efd949e8d536 | 213 | } |