Skip to content

Commit badfb99

Browse files
Code Review Updates
Updates made from codestream code review by Dryw. For more information on specific changes, see Codestream.
1 parent 5aa273a commit badfb99

File tree

12 files changed

+388
-582
lines changed

12 files changed

+388
-582
lines changed

examples/Example1_BasicReadings/Example1_BasicReadings.ino

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,13 @@ void setup()
1010
{
1111
Wire.begin();
1212
// Start serial communication at 115200 baud
13-
Serial.begin(115200);
14-
// Set clock speed to be the fastest for better communication
15-
Wire.setClock(1000000);
13+
Serial.begin(115200);
1614

1715
// Begin example of the magnetic sensor code (and add whitespace for easy reading)
1816
Serial.println("TMAG5273 Example 1: Basic Readings");
1917
Serial.println("");
20-
// If begin is successful (0), then start example
21-
22-
Serial.println(sensor.getManufacturerID(), HEX);
2318

19+
// If begin is successful (0), then start example
2420
if(sensor.begin(i2cAddress, Wire) == 1)
2521
{
2622
Serial.println("Begin");

examples/Example4_Interrupts/Example4_Interrupts.ino renamed to examples/Example2_Interrupts/Example2_Interrupts.ino

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,14 @@ void setup()
2525
Wire.begin();
2626
// Start serial communication at 115200 baud
2727
Serial.begin(115200);
28-
// Set clock speed to be the fastest for better communication
29-
Wire.setClock(1000000);
3028

3129
// Configure Interrupt Pin
32-
pinMode(intPin, OUTPUT);
30+
pinMode(intPin, INPUT);
3331
// Attach interrupt to pin 4 as a digital, falling pin
3432
attachInterrupt(digitalPinToInterrupt(intPin), isr1, CHANGE);
3533

3634
// Begin example of the magnetic sensor code (and add whitespace for easy reading)
37-
Serial.println("TMAG5273 Example 4: Interrupts");
35+
Serial.println("TMAG5273 Example 2: Interrupts");
3836
Serial.println("");
3937
// If begin is successful (0), then start example
4038
if (sensor.begin(i2cAddress, Wire) == true)
@@ -48,16 +46,16 @@ void setup()
4846
}
4947

5048
// Set interrupt through !INT
51-
sensor.setInterruptMode(INTERRUPT_THROUGH_INT);
49+
sensor.setInterruptMode(TMAG5273_INTERRUPT_THROUGH_INT);
5250

5351
// Set the !INT pin state - pulse for 10us
54-
sensor.setIntPinState(1);
52+
sensor.setIntPinState(true);
5553

5654
// Enable the interrupt response for the thresholds
57-
sensor.setThresholdEn(1);
55+
sensor.setThresholdEn(true);
5856

59-
int pinStatus = sensor.getInterruptPinStatus();
60-
pinMode(4, INPUT);
57+
//int pinStatus = sensor.getInterruptPinStatus();
58+
pinMode(intPin, INPUT);
6159

6260
// Set X, Y, Z, and T Thresholds for interrupt to be triggered
6361
sensor.setXThreshold(5); // mT
@@ -84,7 +82,7 @@ void loop()
8482

8583
int xThresh = sensor.getXThreshold();
8684
Serial.print("X Threshold: ");
87-
Serial.println(zxThresh);
85+
Serial.println(xThresh);
8886

8987
if (sensor.getMagneticChannel() != 0) // Checks if mag channels are on - turns on in setup
9088
{
@@ -100,6 +98,8 @@ void loop()
10098
Serial.print(", ");
10199
Serial.print(magZ);
102100
Serial.println(") mT");
101+
Serial.print(temp);
102+
Serial.println(" °C");
103103
}
104104
else
105105
{

examples/Example5_LowPower/Example5_LowPower.ino renamed to examples/Example3_LowPower/Example3_LowPower.ino

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
TMAG5273 sensor; // Initialize hall-effect sensor
55

66
// I2C default address
7-
uint8_t i2cAddress = I2C_ADDRESS_INITIAL;
7+
uint8_t i2cAddress = TMAG5273_I2C_ADDRESS_INITIAL;
88

99
// Interrupt pin used
1010
// NOTE: This pin is specific to the SparkFun IoT Motor Driver
@@ -14,7 +14,7 @@ uint8_t intPin = 4;
1414
volatile bool interruptFlag = false;
1515

1616
// Sleeptime constant
17-
int sleeptime = 0x1;
17+
int sleeptime = TMAG5273_SLEEP_5MS;
1818

1919
// ISR to print what interrupt was triggered
2020
void isr1()
@@ -28,16 +28,10 @@ void setup()
2828
Wire.begin();
2929
// Start serial communication at 115200 baud
3030
Serial.begin(115200);
31-
// Set clock speed to be the fastest for better communication
32-
Wire.setClock(1000000);
3331

34-
// Configure Interrupt Pin
35-
pinMode(intPin, INPUT);
36-
// Attach interrupt to pin 4 as a digital, falling pin
37-
attachInterrupt(digitalPinToInterrupt(intPin), isr1, FALLING);
3832

3933
// Begin example of the magnetic sensor code (and add whitespace for easy reading)
40-
Serial.println("TMAG5273 Example 5: Low Power / Wake-Up and Sleep");
34+
Serial.println("TMAG5273 Example 3: Low Power / Wake-Up and Sleep");
4135
Serial.println("");
4236
// If begin is successful (0), then start example
4337
if(sensor.begin(i2cAddress, Wire) == true)
@@ -50,17 +44,19 @@ void setup()
5044
while(1); // Runs forever
5145
}
5246

53-
// Setup delay for race case
54-
delay(500);
47+
// Configure Interrupt Pin
48+
pinMode(intPin, OUTPUT);
49+
// Attach interrupt to pin 4 as a digital, falling pin
50+
attachInterrupt(digitalPinToInterrupt(intPin), isr1, FALLING);
5551

56-
// Set operating mode
57-
sensor.setOperatingMode(3); // 0X3
52+
// Set operating mode to wake up and sleep
53+
sensor.setOperatingMode(TMAG5273_WAKE_UP_AND_SLEEP_MODE); // 0X3
5854

5955
// Set interrupt through !INT
60-
sensor.setInterruptMode(INTERRUPT_THROUGH_INT);
56+
sensor.setInterruptMode(TMAG5273_INTERRUPT_THROUGH_INT);
6157

6258
// Set the !INT pin state - low until cleared
63-
sensor.setIntPinState(0);
59+
sensor.setIntPinState(false);
6460

6561
// Set time spent in low power mode between conversions
6662
sensor.setSleeptime(sleeptime);
@@ -84,7 +80,7 @@ void loop()
8480
if(interruptFlag == true)
8581
{
8682
// Assert flag back to false
87-
interruptFlag == false;
83+
interruptFlag = false;
8884

8985
if(sensor.readWakeUpAndSleepData(&magX, &magY, &magZ, &temperatureData) == 0)
9086
{
@@ -105,7 +101,7 @@ void loop()
105101
}
106102

107103
// Set operating mode back to wakeup and sleep
108-
sensor.setOperatingMode(3);
104+
sensor.setOperatingMode(TMAG5273_WAKE_UP_AND_SLEEP_MODE);
109105

110106
delay(500);
111107

examples/Example6_I2CSettings/Example6_I2CSettings.ino renamed to examples/Example4_I2CSettings/Example4_I2CSettings.ino

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,9 @@ void setup()
1111
Wire.begin();
1212
// Start serial communication at 115200 baud
1313
Serial.begin(115200);
14-
// Set clock speed to be the fastest for better communication
15-
Wire.setClock(1000000);
1614

1715
// Begin example of the magnetic sensor code (and add whitespace for easy reading)
18-
Serial.println("TMAG5273 Example 6: I2C Settings - Address Change");
16+
Serial.println("TMAG5273 Example 4: I2C Settings - Address Change");
1917
Serial.println("");
2018

2119
// If begin is successful (1), then start example
@@ -29,14 +27,13 @@ void setup()
2927

3028
while(1); // Runs forever
3129
}
32-
33-
// Race Case
34-
delay(1000);
3530

3631
Serial.print("Original Address: ");
3732
Serial.println(sensor.getI2CAddress(), HEX);
3833

39-
// Change I2C Address - can be any 7-bit value (bits 1-7)
34+
// Change I2C Address - must be 7-bits (bits 1-7)
35+
// NOTE: The I2C address will reset back to default (0x22) after a power cycle.
36+
// Running other examples without a power cycle will fail due to the I2C address being different.
4037
int updatedAddress = 0x23;
4138

4239
// Set the I2C Address

examples/Example3_AngleCalculations/Example3_AngleCalculations.ino renamed to examples/Example5_AngleCalculations/Example5_AngleCalculations.ino

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,12 @@ void setup()
1717
Wire.begin();
1818
// Start serial communication at 115200 baud
1919
Serial.begin(115200);
20-
// Set clock speed to be the fastest for better communication
21-
Wire.setClock(1000000);
2220

2321
// Begin example of the magnetic sensor code (and add whitespace for easy reading)
24-
Serial.println("TMAG5273 Example 3: Angle Calculations");
22+
Serial.println("TMAG5273 Example 5: Angle Calculations");
2523
Serial.println("");
2624
// If begin is successful (0), then start example
27-
if(sensor.begin(0X22, Wire) == true)
25+
if(sensor.begin(i2cAddress, Wire) == true)
2826
{
2927
Serial.println("Begin");
3028
}

examples/Example2_IoT_Motor_Driver_Readings/Example2_IoT_Motor_Driver_Readings.ino renamed to examples/Example6_IoT_Motor_Driver_Readings/Example6_IoT_Motor_Driver_Readings.ino

Lines changed: 11 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
#include <Wire.h> // Used to establish serial communication on the I2C bus
1+
#include <Wire.h> // Used to establish serial communication on the I2C bus
22
#include "SparkFun_TMAG5273_Arduino_Library.h" // Used to send and recieve specific information from our sensor
3-
#include <SimpleFOC.h>
3+
4+
// To download the Arduino Simple FOC Library, use this link: https://docs.simplefoc.com/arduino_simplefoc_library_showcase
5+
#include <SimpleFOC.h>
46

57
/* For easy reading with this example, try opening the serial plotter! */
68

@@ -16,56 +18,23 @@ BLDCMotor motor = BLDCMotor(8);
1618
// BLDCDriver6PWM(int phA_h,int phA_l,int phB_h,int phB_l,int phC_h,int phC_l, int en)
1719
BLDCDriver6PWM driver = BLDCDriver6PWM(16,17, 18,23, 19,33, 34);
1820

19-
//target variable
20-
float target_velocity = 3;
21+
//target variable in rad/s (radians per second)
22+
float target_velocity_rps = 3;
2123

2224
// instantiate the commander
2325
Commander command = Commander(Serial);
24-
void doTarget(char* cmd) { command.scalar(&target_velocity, cmd); }
26+
void doTarget(char* cmd) { command.scalar(&target_velocity_rps, cmd); }
2527
void doLimit(char* cmd) { command.scalar(&motor.voltage_limit, cmd); }
2628

27-
// Checkout peripherals (aux buttons, leds, etc)
28-
int aux1Pin = 14;
29-
int aux2Pin = 13;
30-
int ledPin = 2;
31-
32-
// Use as the current pin in test for lighting LED test circuit
33-
int testPin = 12;
34-
35-
// Creates a class for the button information to be stored in for the ISR
36-
struct Button{
37-
const uint8_t PIN;
38-
uint32_t numberKeyPresses;
39-
bool pressed;
40-
};
41-
42-
// Creates an instance of each button and their pin on the ESP32
43-
Button aux1 = {14, 0, false};
44-
Button aux2 = {13, 0, false};
45-
46-
// Button 1 ISR to increment button count and update status of press
47-
void IRAM_ATTR isr1(){
48-
aux1.numberKeyPresses++;
49-
aux1.pressed = true;
50-
}
51-
52-
// Button 2 ISR to increment button count and update status of press
53-
void IRAM_ATTR isr2(){
54-
aux2.numberKeyPresses++;
55-
aux2.pressed = true;
56-
}
57-
5829

5930
void setup()
6031
{
6132
Wire.begin();
6233
// Start serial communication at 115200 baud
6334
Serial.begin(115200);
64-
// Set clock speed to be the fastest for better communication
65-
Wire.setClock(1000000);
6635

6736
// Begin example of the magnetic sensor code
68-
Serial.println("TMAG5273 Example 2: Basic Readings with the IoT Motor Driver");
37+
Serial.println("TMAG5273 Example 6: Basic Readings with the IoT Motor Driver");
6938

7039
// ********* Driver config *********
7140
// power supply voltage [V]
@@ -90,20 +59,10 @@ void setup()
9059

9160
Serial.println("Motor ready!");
9261
Serial.print("Set target velocity [rad/s]: ");
93-
Serial.println(target_velocity);
62+
Serial.println(target_velocity_rps);
9463
_delay(1000);
9564

96-
// Making RGB LED an output
97-
pinMode(ledPin, OUTPUT); // RGB LED
98-
99-
// Aux pins as an interrupt
100-
pinMode(aux1.PIN, INPUT_PULLUP); // Sets pin 14 on the ESP32 as an interrupt
101-
attachInterrupt(aux1.PIN, isr1, FALLING); // Triggers when aux1 is pulled to GND (button pressed)
102-
pinMode(aux2.PIN, INPUT_PULLUP); // Sets pin 13 on the ESP32 as an interrupt
103-
attachInterrupt(aux2.PIN, isr2, FALLING); // Triggers when aux2 is pulled to GND (button pressed)
104-
105-
106-
// If begin is successful (1), then start example
65+
// If the TMAG5273begin is successful (1), then start example
10766
if(sensor.begin(i2cAddress, Wire) == true)
10867
{
10968
Serial.println("Begin");
@@ -114,31 +73,13 @@ void setup()
11473
while(1); // Runs forever
11574
}
11675

117-
// Delay for race case setup
118-
delay(500);
11976
}
12077

12178

12279
void loop()
12380
{
124-
// Variable for temperature
125-
float temp = sensor.getTemp();
126-
// Button Press ISR
127-
if(aux1.pressed)
128-
{
129-
Serial.print("Temperature: ");
130-
Serial.println(temp);
131-
aux1.pressed = false;
132-
delay(1000);
133-
}
134-
if(aux2.pressed)
135-
{
136-
Serial.println("Changing direction.. ");
137-
aux2.pressed = false;
138-
}
139-
14081
// Basic motor movement
141-
motor.move(target_velocity);
82+
motor.move(target_velocity_rps);
14283

14384
// user communication
14485
command.run();

0 commit comments

Comments
 (0)