Skip to content

feat: add linear hall position sensing #164

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/**
* An example to find the center offsets for both ADC channels used in the LinearHall sensor constructor
* Spin your motor through at least one full revolution to average out all of the variations in magnet strength.
*/

//Change these defines to match the analog input pins that your hall sensors are connected to
#define LINEAR_HALL_CHANNEL_A 39
#define LINEAR_HALL_CHANNEL_B 33


//program variables
int minA, maxA, minB, maxB, centerA, centerB;
unsigned long timestamp;

void setup() {
// monitoring port
Serial.begin(115200);

// initialise magnetic sensor hardware
pinMode(LINEAR_HALL_CHANNEL_A, INPUT);
pinMode(LINEAR_HALL_CHANNEL_B, INPUT);

minA = analogRead(LINEAR_HALL_CHANNEL_A);
maxA = minA;
centerA = (minA + maxA) / 2;
minB = analogRead(LINEAR_HALL_CHANNEL_B);
maxB = minB;
centerB = (minB + maxB) / 2;

Serial.println("Sensor ready");
delay(1000);
timestamp = millis();
}

void loop() {
//read sensors and update variables
int tempA = analogRead(LINEAR_HALL_CHANNEL_A);
if (tempA < minA) minA = tempA;
if (tempA > maxA) maxA = tempA;
centerA = (minA + maxA) / 2;
int tempB = analogRead(LINEAR_HALL_CHANNEL_B);
if (tempB < minB) minB = tempB;
if (tempB > maxB) maxB = tempB;
centerB = (minB + maxB) / 2;

if (millis() > timestamp + 100) {
timestamp = millis();
// display the center counts, and max and min count
Serial.print("A:");
Serial.print(centerA);
Serial.print("\t, B:");
Serial.print(centerB);
Serial.print("\t, min A:");
Serial.print(minA);
Serial.print("\t, max A:");
Serial.print(maxA);
Serial.print("\t, min B:");
Serial.print(minB);
Serial.print("\t, max B:");
Serial.println(maxB);
}
}
2 changes: 2 additions & 0 deletions src/BLDCMotor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ int BLDCMotor::alignSensor() {
for (int i = 0; i <=500; i++ ) {
float angle = _3PI_2 + _2PI * i / 500.0f;
setPhaseVoltage(voltage_sensor_align, 0, angle);
sensor->update();
_delay(2);
}
// take and angle in the middle
Expand All @@ -183,6 +184,7 @@ int BLDCMotor::alignSensor() {
for (int i = 500; i >=0; i-- ) {
float angle = _3PI_2 + _2PI * i / 500.0f ;
setPhaseVoltage(voltage_sensor_align, 0, angle);
sensor->update();
_delay(2);
}
sensor->update();
Expand Down