|
| 1 | +--- |
| 2 | +layout: default |
| 3 | +title: Generic Current Sense |
| 4 | +description: "Arduino Simple Field Oriented Control (FOC) library ." |
| 5 | +permalink: /generic_current_sense |
| 6 | +nav_order: 4 |
| 7 | +parent: Current Sensing |
| 8 | +grand_parent: Writing the Code |
| 9 | +grand_grand_parent: Arduino <span class="simple">Simple<span class="foc">FOC</span>library</span> |
| 10 | +toc: true |
| 11 | +--- |
| 12 | + |
| 13 | + |
| 14 | + |
| 15 | +# Implementing a custom current sense |
| 16 | + |
| 17 | +`GenericCurrentSense` is a helper class that makes it easy to integrate a custom current-sensing setup into the <span class="simple">Simple<span class="foc">FOC</span>library</span>. You provide callbacks to read phase currents (and optionally initialize your hardware), and the library handles offsets, filtering, and integration with FOC control. |
| 18 | + |
| 19 | +For advanced users, you can also implement your own class by inheriting `CurrentSense`, but `GenericCurrentSense` is usually faster to get working. |
| 20 | + |
| 21 | +[See library source code for more details (Advanced)](current_sense_support){: .btn .btn-docs} |
| 22 | + |
| 23 | +## Step 1. Implement the current-reading callback |
| 24 | + |
| 25 | +Provide a function that returns phase currents in **amps**. If you only measure two phases, set the third one to `0`. |
| 26 | + |
| 27 | +```cpp |
| 28 | +PhaseCurrent_s readCurrentSense(){ |
| 29 | + PhaseCurrent_s c; |
| 30 | + // Read your ADCs and convert to amps |
| 31 | + c.a = ...; // phase A current [A] |
| 32 | + c.b = ...; // phase B current [A] |
| 33 | + c.c = ...; // phase C current [A] (set to 0 if not measured) |
| 34 | + return c; |
| 35 | +} |
| 36 | +``` |
| 37 | + |
| 38 | +You can also provide an optional initialization callback: |
| 39 | + |
| 40 | +```cpp |
| 41 | +void initCurrentSense(){ |
| 42 | + // Configure ADCs, GPIOs, or any hardware setup |
| 43 | +} |
| 44 | +``` |
| 45 | + |
| 46 | +## Step 2. Instantiate `GenericCurrentSense` |
| 47 | + |
| 48 | +```cpp |
| 49 | +// GenericCurrentSense constructor |
| 50 | +// - readCallback pointer to the function reading phase currents |
| 51 | +// - initCallback pointer to the optional init function |
| 52 | +GenericCurrentSense current_sense = GenericCurrentSense(readCurrentSense, initCurrentSense); |
| 53 | +``` |
| 54 | + |
| 55 | +If you prefer, you can also assign callbacks after construction: |
| 56 | + |
| 57 | +```cpp |
| 58 | +GenericCurrentSense current_sense; |
| 59 | +current_sense.readCallback = readCurrentSense; |
| 60 | +current_sense.initCallback = initCurrentSense; |
| 61 | +``` |
| 62 | + |
| 63 | +## Step 3. Use as a standalone current sensor |
| 64 | + |
| 65 | +Once initialized, you can read phase currents and DC current directly: |
| 66 | + |
| 67 | +```cpp |
| 68 | +current_sense.init(); |
| 69 | + |
| 70 | +PhaseCurrent_s currents = current_sense.getPhaseCurrents(); |
| 71 | +float current_magnitude = current_sense.getDCCurrent(); |
| 72 | + |
| 73 | +Serial.print(currents.a); |
| 74 | +Serial.print("\t"); |
| 75 | +Serial.print(currents.b); |
| 76 | +Serial.print("\t"); |
| 77 | +Serial.print(currents.c); |
| 78 | +Serial.print("\t"); |
| 79 | +Serial.println(current_magnitude); |
| 80 | +``` |
| 81 | + |
| 82 | +<blockquote markdown="1" class="info"> |
| 83 | +<p class="heading" markdown="1">Offset calibration</p> |
| 84 | +`GenericCurrentSense::init()` automatically runs a zero-offset calibration by sampling your callback multiple times. Make sure the motor is not driven during initialization. |
| 85 | +</blockquote> |
| 86 | + |
| 87 | +## Step 4. Use with FOC control |
| 88 | + |
| 89 | +To use current sensing for torque control (dc_current or foc_current), link it to the motor and driver: |
| 90 | + |
| 91 | +```cpp |
| 92 | +// Driver and motor |
| 93 | +BLDCDriver3PWM driver = BLDCDriver3PWM(9, 10, 11, 8); |
| 94 | +BLDCMotor motor = BLDCMotor(7); |
| 95 | + |
| 96 | +void setup() { |
| 97 | + Serial.begin(115200); |
| 98 | + |
| 99 | + // Driver init |
| 100 | + driver.voltage_power_supply = 12; |
| 101 | + driver.init(); |
| 102 | + motor.linkDriver(&driver); |
| 103 | + |
| 104 | + // Current sense init and link |
| 105 | + current_sense.init(); |
| 106 | + current_sense.linkDriver(&driver); |
| 107 | + motor.linkCurrentSense(¤t_sense); |
| 108 | + |
| 109 | + // Select torque control mode |
| 110 | + motor.torque_controller = TorqueControlType::foc_current; |
| 111 | + // or TorqueControlType::dc_current |
| 112 | + |
| 113 | + motor.init(); |
| 114 | + motor.initFOC(); |
| 115 | +} |
| 116 | + |
| 117 | +void loop() { |
| 118 | + motor.loopFOC(); |
| 119 | + motor.move(); |
| 120 | +} |
| 121 | +``` |
| 122 | + |
| 123 | +## Tips and troubleshooting |
| 124 | + |
| 125 | +- **Units matter**: your callback must return currents in **amps**. |
| 126 | +- **Only two shunts?** Set one of the phases to `0` and the library will estimate it. |
| 127 | +- **Phase inversion**: if a phase appears inverted, you can flip it: |
| 128 | + ```cpp |
| 129 | + current_sense.gain_a *= -1; // or gain_b / gain_c |
| 130 | + ``` |
| 131 | +- **Alignment**: `initFOC()` will not run the alignment for this sensor type, so make sure your callback returns correct angles from the start. |
| 132 | + |
| 133 | +## Example: Generic current sensing test |
| 134 | + |
| 135 | +```cpp |
| 136 | +#include <SimpleFOC.h> |
| 137 | + |
| 138 | +PhaseCurrent_s readCurrentSense(){ |
| 139 | + PhaseCurrent_s c; |
| 140 | + c.a = analogRead(A0); |
| 141 | + c.b = analogRead(A1); |
| 142 | + c.c = analogRead(A2); |
| 143 | + return c; |
| 144 | +} |
| 145 | + |
| 146 | +void initCurrentSense(){ |
| 147 | + pinMode(A0, INPUT); |
| 148 | + pinMode(A1, INPUT); |
| 149 | + pinMode(A2, INPUT); |
| 150 | +} |
| 151 | + |
| 152 | +GenericCurrentSense current_sense = GenericCurrentSense(readCurrentSense, initCurrentSense); |
| 153 | + |
| 154 | +void setup() { |
| 155 | + Serial.begin(115200); |
| 156 | + current_sense.init(); |
| 157 | + Serial.println("Current sense ready"); |
| 158 | +} |
| 159 | + |
| 160 | +void loop() { |
| 161 | + PhaseCurrent_s currents = current_sense.getPhaseCurrents(); |
| 162 | + float current_magnitude = current_sense.getDCCurrent(); |
| 163 | + |
| 164 | + Serial.print(currents.a); |
| 165 | + Serial.print("\t"); |
| 166 | + Serial.print(currents.b); |
| 167 | + Serial.print("\t"); |
| 168 | + Serial.print(currents.c); |
| 169 | + Serial.print("\t"); |
| 170 | + Serial.println(current_magnitude); |
| 171 | +} |
| 172 | +``` |
0 commit comments