fw: DRV8316 SPI init
This commit is contained in:
parent
56938730b5
commit
4c3b8334dd
BIN
cad/standoff.FCStd
Normal file
BIN
cad/standoff.FCStd
Normal file
Binary file not shown.
BIN
cad/standoff.stl
Normal file
BIN
cad/standoff.stl
Normal file
Binary file not shown.
139
fw/src/main.cpp
139
fw/src/main.cpp
@ -11,6 +11,10 @@
|
|||||||
#include "SimpleFOCDrivers.h"
|
#include "SimpleFOCDrivers.h"
|
||||||
#include "encoders/esp32hwencoder/ESP32HWEncoder.h"
|
#include "encoders/esp32hwencoder/ESP32HWEncoder.h"
|
||||||
|
|
||||||
|
#include "motors/HybridStepperMotor/HybridStepperMotor.h"
|
||||||
|
|
||||||
|
#include "drivers/drv8316/drv8316.h"
|
||||||
|
|
||||||
#include <cmath>
|
#include <cmath>
|
||||||
|
|
||||||
// #include "encoders/sc60228/MagneticSensorSC60228.h"
|
// #include "encoders/sc60228/MagneticSensorSC60228.h"
|
||||||
@ -49,8 +53,6 @@
|
|||||||
#define SPI_CLK 15
|
#define SPI_CLK 15
|
||||||
#define SPI_DRV_SC 16
|
#define SPI_DRV_SC 16
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#define MOTOR
|
#define MOTOR
|
||||||
#define SSIF_USBSERIAL
|
#define SSIF_USBSERIAL
|
||||||
#define FW_NO_WATCHDOG
|
#define FW_NO_WATCHDOG
|
||||||
@ -65,16 +67,17 @@
|
|||||||
// };
|
// };
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
const float voltage_factor = 19.31;
|
|
||||||
const int voltage_lpf = 50;
|
const int voltage_lpf = 50;
|
||||||
const float anglefactor = 10000.0;
|
|
||||||
// BLDC motor & driver instance
|
// BLDC motor & driver instance
|
||||||
// BLDCMotor motor = BLDCMotor(pole pair number);
|
// BLDCMotor motor = BLDCMotor(pole pair number);
|
||||||
ESP32HWEncoder encoder = ESP32HWEncoder(ENC_A, ENC_B, 2048); // The Index pin can be omitted
|
ESP32HWEncoder encoder =
|
||||||
|
ESP32HWEncoder(ENC_A, ENC_B, 2048 / 2); // The Index pin can be omitted
|
||||||
|
|
||||||
#ifdef MOTOR
|
#ifdef MOTOR
|
||||||
BLDCMotor motor = BLDCMotor(11); // 24N22P
|
BLDCMotor motor = BLDCMotor(11); // 24N22P
|
||||||
BLDCDriver3PWM driver = BLDCDriver3PWM(INHA,INHB,INHC);
|
// HybridStepperMotor motor = HybridStepperMotor(50, 3.7, 10, 4.5 / 1000);
|
||||||
|
DRV8316Driver3PWM driver = DRV8316Driver3PWM(INHA, INHB, INHC, SPI_DRV_SC);
|
||||||
|
// BLDCDriver3PWM driver = BLDCDriver3PWM(INHA,INHB,INHC);
|
||||||
#endif
|
#endif
|
||||||
// BLDCDriver3PWM driver = BLDCDriver3PWM(pwmA, pwmB, pwmC, Enable(optional));
|
// BLDCDriver3PWM driver = BLDCDriver3PWM(pwmA, pwmB, pwmC, Enable(optional));
|
||||||
|
|
||||||
@ -119,8 +122,7 @@ void write_i2c(uint8_t addr, uint8_t reg_lsb, uint8_t val) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
void write_dual_i2c(uint8_t addr, uint8_t reg_lsb, uint8_t val1, uint8_t
|
void write_dual_i2c(uint8_t addr, uint8_t reg_lsb, uint8_t val1, uint8_t val2) {
|
||||||
val2) {
|
|
||||||
Wire.beginTransmission(addr);
|
Wire.beginTransmission(addr);
|
||||||
uint8_t data[] = {reg_lsb, val1, val2};
|
uint8_t data[] = {reg_lsb, val1, val2};
|
||||||
Wire.write(data, sizeof data);
|
Wire.write(data, sizeof data);
|
||||||
@ -131,6 +133,60 @@ val2) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void status() {
|
||||||
|
|
||||||
|
DRV8316Status status = driver.getStatus();
|
||||||
|
SSYNCIF.println("DRV8316 Status:");
|
||||||
|
SSYNCIF.print("Fault: ");
|
||||||
|
SSYNCIF.println(status.isFault());
|
||||||
|
SSYNCIF.print("Buck Error: ");
|
||||||
|
SSYNCIF.print(status.isBuckError());
|
||||||
|
SSYNCIF.print(" Undervoltage: ");
|
||||||
|
SSYNCIF.print(status.isBuckUnderVoltage());
|
||||||
|
SSYNCIF.print(" OverCurrent: ");
|
||||||
|
SSYNCIF.println(status.isBuckOverCurrent());
|
||||||
|
SSYNCIF.print("Charge Pump UnderVoltage: ");
|
||||||
|
SSYNCIF.println(status.isChargePumpUnderVoltage());
|
||||||
|
SSYNCIF.print("OTP Error: ");
|
||||||
|
SSYNCIF.println(status.isOneTimeProgrammingError());
|
||||||
|
SSYNCIF.print("OverCurrent: ");
|
||||||
|
SSYNCIF.print(status.isOverCurrent());
|
||||||
|
SSYNCIF.print(" Ah: ");
|
||||||
|
SSYNCIF.print(status.isOverCurrent_Ah());
|
||||||
|
SSYNCIF.print(" Al: ");
|
||||||
|
SSYNCIF.print(status.isOverCurrent_Al());
|
||||||
|
SSYNCIF.print(" Bh: ");
|
||||||
|
SSYNCIF.print(status.isOverCurrent_Bh());
|
||||||
|
SSYNCIF.print(" Bl: ");
|
||||||
|
SSYNCIF.print(status.isOverCurrent_Bl());
|
||||||
|
SSYNCIF.print(" Ch: ");
|
||||||
|
SSYNCIF.print(status.isOverCurrent_Ch());
|
||||||
|
SSYNCIF.print(" Cl: ");
|
||||||
|
SSYNCIF.println(status.isOverCurrent_Cl());
|
||||||
|
SSYNCIF.print("OverTemperature: ");
|
||||||
|
SSYNCIF.print(status.isOverTemperature());
|
||||||
|
SSYNCIF.print(" Shutdown: ");
|
||||||
|
SSYNCIF.print(status.isOverTemperatureShutdown());
|
||||||
|
SSYNCIF.print(" Warning: ");
|
||||||
|
SSYNCIF.println(status.isOverTemperatureWarning());
|
||||||
|
SSYNCIF.print("OverVoltage: ");
|
||||||
|
SSYNCIF.println(status.isOverVoltage());
|
||||||
|
SSYNCIF.print("PowerOnReset: ");
|
||||||
|
SSYNCIF.println(status.isPowerOnReset());
|
||||||
|
SSYNCIF.print("SPI Error: ");
|
||||||
|
SSYNCIF.print(status.isSPIError());
|
||||||
|
SSYNCIF.print(" Address: ");
|
||||||
|
SSYNCIF.print(status.isSPIAddressError());
|
||||||
|
SSYNCIF.print(" Clock: ");
|
||||||
|
SSYNCIF.print(status.isSPIClockFramingError());
|
||||||
|
SSYNCIF.print(" Parity: ");
|
||||||
|
SSYNCIF.println(status.isSPIParityError());
|
||||||
|
DRV8316_PWMMode val = driver.getPWMMode();
|
||||||
|
SSYNCIF.print("PWM Mode: ");
|
||||||
|
SSYNCIF.println(val);
|
||||||
|
SSYNCIF.print("Recirculation: ");
|
||||||
|
SSYNCIF.println(driver.getRecirculationMode());
|
||||||
|
}
|
||||||
// Preferences prefs;
|
// Preferences prefs;
|
||||||
|
|
||||||
SemaphoreHandle_t mutex;
|
SemaphoreHandle_t mutex;
|
||||||
@ -177,17 +233,38 @@ void setup() {
|
|||||||
// SSYNCIF.print(__cplusplus);
|
// SSYNCIF.print(__cplusplus);
|
||||||
|
|
||||||
// Wire.setTimeOut(10);
|
// Wire.setTimeOut(10);
|
||||||
// SPI.begin(SPI_CLK, SPI_MISO, SPI_MOSI, SPI_CS1);
|
|
||||||
Wire.begin(I2C_SDA, I2C_SCL, 400000);
|
Wire.begin(I2C_SDA, I2C_SCL, 400000);
|
||||||
|
|
||||||
|
pinMode(DRVOFF, OUTPUT);
|
||||||
|
digitalWrite(DRVOFF, 0); // enable
|
||||||
|
pinMode(INLA, OUTPUT);
|
||||||
|
digitalWrite(INLA, 1); // enable
|
||||||
|
pinMode(INLB, OUTPUT);
|
||||||
|
digitalWrite(INLB, 1); // enable
|
||||||
|
pinMode(INLC, OUTPUT);
|
||||||
|
digitalWrite(INLC, 1); // enable
|
||||||
|
|
||||||
// write_i2c(0x36,0x07, 0b00000011);
|
// write_i2c(0x36,0x07, 0b00000011);
|
||||||
write_i2c(0x36,0x09, 0b1000);
|
write_i2c(0x36, 0x09, 0xFF);
|
||||||
|
|
||||||
encoder.init();
|
encoder.init();
|
||||||
|
#ifdef MOTOR
|
||||||
|
|
||||||
|
driver.voltage_power_supply = 15;
|
||||||
|
SPI.begin(SPI_CLK, SPI_MISO, SPI_MOSI, SPI_DRV_SC);
|
||||||
|
driver.init(&SPI);
|
||||||
|
driver.setSlew(DRV8316_Slew::Slew_200Vus);
|
||||||
|
status();
|
||||||
|
motor.linkDriver(&driver);
|
||||||
|
// motor.linkSensor(&encoder);
|
||||||
|
motor.foc_modulation = FOCModulationType::SpaceVectorPWM;
|
||||||
|
motor.voltage_sensor_align = 5;
|
||||||
|
motor.voltage_limit = 6;
|
||||||
|
motor.controller = MotionControlType::velocity_openloop;
|
||||||
|
motor.init();
|
||||||
|
// motor.initFOC();
|
||||||
|
|
||||||
// sensor.init(&SPI);
|
// sensor.init(&SPI);
|
||||||
#ifdef MOTOR
|
|
||||||
// motors[i].linkSensor(&sensors[i]);
|
// motors[i].linkSensor(&sensors[i]);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
@ -260,15 +337,18 @@ void setup() {
|
|||||||
// // if (prefs.isKey(pref_zero_key.c_str()) &&
|
// // if (prefs.isKey(pref_zero_key.c_str()) &&
|
||||||
// // prefs.isKey(pref_dir_key.c_str())) {
|
// // prefs.isKey(pref_dir_key.c_str())) {
|
||||||
// // motors[i].zero_electric_angle =
|
// // motors[i].zero_electric_angle =
|
||||||
// // prefs.getFloat(pref_zero_key.c_str()); motors[i].sensor_direction =
|
// // prefs.getFloat(pref_zero_key.c_str()); motors[i].sensor_direction
|
||||||
|
// =
|
||||||
// // (Direction)prefs.getInt(pref_dir_key.c_str());
|
// // (Direction)prefs.getInt(pref_dir_key.c_str());
|
||||||
// // }
|
// // }
|
||||||
// motors[i].initFOC();
|
// motors[i].initFOC();
|
||||||
// // if (!prefs.isKey(pref_zero_key.c_str()) ||
|
// // if (!prefs.isKey(pref_zero_key.c_str()) ||
|
||||||
// // !prefs.isKey(pref_dir_key.c_str())) {
|
// // !prefs.isKey(pref_dir_key.c_str())) {
|
||||||
// // prefs.clear();
|
// // prefs.clear();
|
||||||
// // prefs.putFloat(pref_zero_key.c_str(), motors[i].zero_electric_angle);
|
// // prefs.putFloat(pref_zero_key.c_str(),
|
||||||
// // prefs.putInt(pref_dir_key.c_str(), (int)motors[i].sensor_direction);
|
// motors[i].zero_electric_angle);
|
||||||
|
// // prefs.putInt(pref_dir_key.c_str(),
|
||||||
|
// (int)motors[i].sensor_direction);
|
||||||
// // }
|
// // }
|
||||||
// }
|
// }
|
||||||
#endif
|
#endif
|
||||||
@ -280,10 +360,9 @@ void setup() {
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
// xTaskCreatePinnedToCore(&comm_task, // Function name of the task
|
// xTaskCreatePinnedToCore(&comm_task, // Function name of the task
|
||||||
// "Comm", // Name of the task (e.g. for debugging)
|
// "Comm", // Name of the task (e.g. for
|
||||||
// 4096, // Stack size (bytes)
|
// debugging) 4096, // Stack size (bytes) NULL,
|
||||||
// NULL, // Parameter to pass
|
// // Parameter to pass 1, // Task priority
|
||||||
// 1, // Task priority
|
|
||||||
// &taskCommHandle, // Assign task handle
|
// &taskCommHandle, // Assign task handle
|
||||||
// 1 // Run on the non-Arduino (1) core
|
// 1 // Run on the non-Arduino (1) core
|
||||||
// );
|
// );
|
||||||
@ -299,13 +378,25 @@ void setup() {
|
|||||||
// esp_task_wdt_add(NULL); // add current thread to WDT watch
|
// esp_task_wdt_add(NULL); // add current thread to WDT watch
|
||||||
}
|
}
|
||||||
|
|
||||||
int i = 0;
|
|
||||||
void loop() {
|
void loop() {
|
||||||
|
#ifdef MOTOR
|
||||||
|
// motor.loopFOC();
|
||||||
|
|
||||||
encoder.update(); // optional: Update manually if not using loopfoc()
|
motor.move(3);
|
||||||
|
#endif
|
||||||
// Access the sensor value
|
// delay(100);
|
||||||
SSYNCIF.println(encoder.getAngle());
|
// static int i = 0;
|
||||||
delay(10);
|
// if (i++ % 100 == 0)
|
||||||
|
// status();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// int i = 0;
|
||||||
|
// void loop() {
|
||||||
|
|
||||||
|
// //encoder.update(); // optional: Update manually if not using loopfoc()
|
||||||
|
// //
|
||||||
|
// // Access the sensor value
|
||||||
|
// SSYNCIF.println(encoder.getAngle());
|
||||||
|
// delay(10);
|
||||||
|
// }
|
||||||
// esp_task_wdt_reset();
|
// esp_task_wdt_reset();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user