Skip to content

Commit d429148

Browse files
committed
refactored examples
1 parent bd21d2b commit d429148

File tree

9 files changed

+82
-21
lines changed

9 files changed

+82
-21
lines changed

examples/hardware_specific_examples/SimpleFOCShield/version_v3/single_full_control_example/single_full_control_example.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ void doB(){encoder.handleB();}
1313

1414
// inline current sensor instance
1515
// ACS712-05B has the resolution of 0.185mV per Amp
16-
InlineCurrentSense current_sense = InlineCurrentSense(1.0f, 0.185f, A0, A2);
16+
InlineCurrentSense current_sense = InlineCurrentSense(185.0f, A0, A2);
1717

1818
// commander communication instance
1919
Commander command = Commander(Serial);

examples/motion_control/open_loop_motor_control/open_loop_velocity_example/open_loop_velocity_example.ino

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,10 @@ void setup() {
3636
// as a protection measure for the low-resistance motors
3737
// this value is fixed on startup
3838
driver.voltage_limit = 6;
39-
driver.init();
39+
if(!driver.init()){
40+
Serial.println("Driver init failed!");
41+
return;
42+
}
4043
// link the motor and the driver
4144
motor.linkDriver(&driver);
4245

@@ -50,7 +53,11 @@ void setup() {
5053
motor.controller = MotionControlType::velocity_openloop;
5154

5255
// init motor hardware
53-
motor.init();
56+
if(!motor.init()){
57+
Serial.println("Motor init failed!");
58+
return;
59+
}
60+
5461

5562
// add target command T
5663
command.add('T', doTarget, "target velocity");

examples/utils/current_sense_test/inline_current_sense_test/inline_current_sense_test.ino

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@ void setup() {
1919
SimpleFOCDebug::enable(&Serial);
2020

2121
// initialise the current sensing
22-
current_sense.init();
22+
if(!current_sense.init()){
23+
Serial.println("Current sense init failed.");
24+
return;
25+
}
2326

2427
// for SimpleFOCShield v2.01/v2.0.2
2528
current_sense.gain_b *= -1;

examples/utils/driver_standalone_test/bldc_driver_3pwm_standalone/bldc_driver_3pwm_standalone.ino

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,14 @@ void setup() {
2323
driver.voltage_limit = 12;
2424

2525
// driver init
26-
driver.init();
26+
if (!driver.init()){
27+
Serial.println("Driver init failed!");
28+
return;
29+
}
2730

2831
// enable driver
2932
driver.enable();
30-
33+
Serial.println("Driver ready!");
3134
_delay(1000);
3235
}
3336

examples/utils/driver_standalone_test/bldc_driver_6pwm_standalone/bldc_driver_6pwm_standalone.ino

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,14 @@ void setup() {
2424
driver.dead_zone = 0.05f;
2525

2626
// driver init
27-
driver.init();
27+
if (!driver.init()){
28+
Serial.println("Driver init failed!");
29+
return;
30+
}
2831

2932
// enable driver
3033
driver.enable();
31-
34+
Serial.println("Driver ready!");
3235
_delay(1000);
3336
}
3437

examples/utils/driver_standalone_test/stepper_driver_2pwm_standalone/stepper_driver_2pwm_standalone.ino

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,14 @@ void setup() {
2929
driver.voltage_limit = 12;
3030

3131
// driver init
32-
driver.init();
32+
if (!driver.init()){
33+
Serial.println("Driver init failed!");
34+
return;
35+
}
3336

3437
// enable driver
3538
driver.enable();
36-
39+
Serial.println("Driver ready!");
3740
_delay(1000);
3841
}
3942

examples/utils/driver_standalone_test/stepper_driver_4pwm_standalone/stepper_driver_4pwm_standalone.ino

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,14 @@ void setup() {
2424
driver.voltage_limit = 12;
2525

2626
// driver init
27-
driver.init();
27+
if (!driver.init()){
28+
Serial.println("Driver init failed!");
29+
return;
30+
}
2831

2932
// enable driver
3033
driver.enable();
31-
34+
Serial.println("Driver ready!");
3235
_delay(1000);
3336
}
3437

examples/utils/sensor_test/hall_sensors/hall_sensor_example/hall_sensor_example.ino

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,6 @@
1313
// - pp - pole pairs
1414
HallSensor sensor = HallSensor(2, 3, 4, 14);
1515

16-
// Interrupt routine intialisation
17-
// channel A and B callbacks
18-
void doA(){sensor.handleA();}
19-
void doB(){sensor.handleB();}
20-
void doC(){sensor.handleC();}
21-
22-
2316
void setup() {
2417
// monitoring port
2518
Serial.begin(115200);
@@ -29,8 +22,6 @@ void setup() {
2922

3023
// initialise encoder hardware
3124
sensor.init();
32-
// hardware interrupt enable
33-
sensor.enableInterrupts(doA, doB, doC);
3425

3526
Serial.println("Sensor ready");
3627
_delay(1000);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/**
2+
* Hall sensor example code
3+
*
4+
* This is a code intended to test the hall sensors connections and to demonstrate the hall sensor setup.
5+
*
6+
*/
7+
8+
#include <SimpleFOC.h>
9+
10+
// Hall sensor instance
11+
// HallSensor(int hallA, int hallB , int cpr, int index)
12+
// - hallA, hallB, hallC - HallSensor A, B and C pins
13+
// - pp - pole pairs
14+
HallSensor sensor = HallSensor(2, 3, 4, 14);
15+
16+
// Interrupt routine intialisation
17+
// channel A and B callbacks
18+
void doA(){sensor.handleA();}
19+
void doB(){sensor.handleB();}
20+
void doC(){sensor.handleC();}
21+
22+
23+
void setup() {
24+
// monitoring port
25+
Serial.begin(115200);
26+
27+
// check if you need internal pullups
28+
sensor.pullup = Pullup::USE_EXTERN;
29+
30+
// initialise encoder hardware
31+
sensor.init();
32+
// hardware interrupt enable
33+
sensor.enableInterrupts(doA, doB, doC);
34+
35+
Serial.println("Sensor ready");
36+
_delay(1000);
37+
}
38+
39+
void loop() {
40+
// iterative function updating the sensor internal variables
41+
// it is usually called in motor.loopFOC()
42+
sensor.update();
43+
// display the angle and the angular velocity to the terminal
44+
Serial.print(sensor.getAngle());
45+
Serial.print("\t");
46+
Serial.println(sensor.getVelocity());
47+
delay(100);
48+
}

0 commit comments

Comments
 (0)