diff --git a/.github/workflows/arduino-action-core2-compile.yml b/.github/workflows/arduino-action-compile.yml similarity index 88% rename from .github/workflows/arduino-action-core2-compile.yml rename to .github/workflows/arduino-action-compile.yml index 922de740..fe53a612 100644 --- a/.github/workflows/arduino-action-core2-compile.yml +++ b/.github/workflows/arduino-action-compile.yml @@ -40,12 +40,20 @@ jobs: # You may add a suffix behind the fqbn with "|" to specify one board for e.g. different compile options like arduino:avr:uno|trace ############################################################################################################# arduino-boards-fqbn: +<<<<<<< HEAD:.github/workflows/arduino-action-compile.yml + - m5stack:esp32:m5stack_core2 +======= - m5stack:esp32:core2 +>>>>>>> d7b4461c702d44503857638f1447dcb2325f0d64:.github/workflows/arduino-action-core2-compile.yml # Specify parameters for each board. ############################################################################################################# include: +<<<<<<< HEAD:.github/workflows/arduino-action-compile.yml + - arduino-boards-fqbn: m5stack:esp32:m5stack_core2 +======= - arduino-boards-fqbn: m5stack:esp32:core2 +>>>>>>> d7b4461c702d44503857638f1447dcb2325f0d64:.github/workflows/arduino-action-core2-compile.yml platform-url: https://m5stack.oss-cn-shenzhen.aliyuncs.com/resource/arduino/package_m5stack_index.json sketches-exclude: WhistleSwitch,50Hz,SimpleFrequencyDetector diff --git a/README.md b/README.md index 3a4d308f..dbfbe21e 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # M5Core2 Library -[![Arduino Compile](https://github.com/m5stack/M5Core2/actions/workflows/arduino-action-core2-compile.yml/badge.svg)](https://github.com/m5stack/M5Core2/actions/workflows/arduino-action-stickc-compile.yml) +[![Arduino Compile](https://github.com/m5stack/M5Core2/actions/workflows/arduino-action-compile.yml/badge.svg)](https://github.com/m5stack/M5Core2/actions/workflows/arduino-action-compile.yml) [![Arduino Lint](https://github.com/m5stack/M5Core2/actions/workflows/Arduino-Lint-Check.yml/badge.svg)](https://github.com/m5stack/M5Core2/actions/workflows/Arduino-Lint-Check.yml) [![Clang Format](https://github.com/m5stack/M5Core2/actions/workflows/clang-format-check.yml/badge.svg)](https://github.com/m5stack/M5Core2/actions/workflows/clang-format-check.yml) diff --git a/README_cn.md b/README_cn.md index 993f3068..648a8dd8 100644 --- a/README_cn.md +++ b/README_cn.md @@ -1,6 +1,6 @@ # M5Core2 Library -[![Arduino Compile](https://github.com/m5stack/M5Core2/actions/workflows/arduino-action-core2-compile.yml/badge.svg)](https://github.com/m5stack/M5Core2/actions/workflows/arduino-action-stickc-compile.yml) +[![Arduino Compile](https://github.com/m5stack/M5Core2/actions/workflows/arduino-action-compile.yml/badge.svg)](https://github.com/m5stack/M5Core2/actions/workflows/arduino-action-compile.yml) [![Arduino Lint](https://github.com/m5stack/M5Core2/actions/workflows/Arduino-Lint-Check.yml/badge.svg)](https://github.com/m5stack/M5Core2/actions/workflows/Arduino-Lint-Check.yml) [![Clang Format](https://github.com/m5stack/M5Core2/actions/workflows/clang-format-check.yml/badge.svg)](https://github.com/m5stack/M5Core2/actions/workflows/clang-format-check.yml) diff --git a/examples/Module/4EncoderMotor/4EncoderMotor.ino b/examples/Module/4EncoderMotor/4EncoderMotor.ino new file mode 100644 index 00000000..8b1b9502 --- /dev/null +++ b/examples/Module/4EncoderMotor/4EncoderMotor.ino @@ -0,0 +1,148 @@ +/** + * @file DriverSample.ino + * @author SeanKwok (shaoxiang@m5stack.com) + * @brief Module 4EncoderMotor Test Demo. + * @version 0.1 + * @date 2024-01-19 + * + * + * @Hardwares: M5Core2 + Module 4EncoderMotor + * @Platform Version: Arduino M5Stack Board Manager v2.1.0 + * @Dependent Library: + * M5Unified: https://github.com/m5stack/M5Unified + * M5GFX: https://github.com/m5stack/M5GFX + * M5Module4EncoderMotor: https://github.com/m5stack/M5Module-4EncoderMotor + */ + +#include "M5Unified.h" +#include "M5GFX.h" +#include "M5Module4EncoderMotor.h" + +M5Module4EncoderMotor driver; + +#define MAX_RECORD_SIZE 256 + +float amp_record[MAX_RECORD_SIZE] = {0}; +uint8_t record_index = 0; +float amp_value = 0.0f; + +uint8_t avg_filter_level = 20; + +float avg_filter(float *data, int len) { + float sum = 0; + float min = data[0]; + float max = data[0]; + for (int i = 0; i < len; i++) { + if (data[i] < min) { + min = data[i]; + } + if (data[i] > max) { + max = data[i]; + } + sum += data[i]; + } + sum -= min; + sum -= max; + return sum / (len - 2); +} + +void setup() { + M5.begin(); + M5.Display.begin(); + + M5.Display.setTextColor(WHITE); + M5.Display.setTextDatum(top_center); + M5.Display.setFont(&fonts::FreeSansBold12pt7b); + M5.Display.setTextSize(1); + + while (!driver.begin(&Wire1, MODULE_4ENCODERMOTOR_ADDR, 21, 22)) { + Serial.println("Driver Init faild!"); + M5.Display.drawString("Driver Init faild!", 160, 7); + delay(1000); + } + + Serial.println("Driver Init success!"); + M5.Display.clear(); + M5.Display.fillRect(0, 0, 320, 35, 0x27f); + M5.Display.drawString("4Encoder Motor", 160, 7); + M5.Display.setTextDatum(top_left); + + // motor channel 0 -3 + for (uint8_t i = 0; i < 4; i++) { + driver.setMode(i, NORMAL_MODE); + driver.setMotorSpeed(i, 127); + } + M5.Display.drawString("NORMAL MODE", 20, 40 + 35 * 5); +} + +bool direction = true; +int mode = NORMAL_MODE; + +void loop() { + M5.update(); + for (uint8_t i = 0; i < 4; i++) { + M5.Display.fillRect(20, 40 + 35 * i, 300, 35, BLACK); + int32_t encoder_value = driver.getEncoderValue(i); + M5.Display.drawString("CH" + String(i) + ": " + String(encoder_value), + 20, 40 + 35 * i); + } + + if (avg_filter_level != 0) { + amp_record[record_index] = driver.getMotorCurrent(); + record_index++; + if (record_index >= avg_filter_level) { + record_index = 0; + } + amp_value = avg_filter(amp_record, avg_filter_level); + } + + float voltage = driver.getAnalogInput(_8bit) / 255.0 * 3.3 / 0.16; + float current = amp_value; + + M5.Display.fillRect(20, 40 + 35 * 4, 300, 35, BLACK); + M5.Display.drawString( + "POWER: " + String(voltage) + "V/" + String(current) + "A", 20, + 40 + 35 * 4); + + if (M5.BtnA.wasClicked() || + (M5.Touch.getCount() && M5.Touch.getDetail(0).wasClicked())) { + mode++; + if (mode > SPEED_MODE) { + mode = NORMAL_MODE; + } + M5.Display.fillRect(20, 40 + 35 * 5, 300, 35, BLACK); + + switch (mode) { + case NORMAL_MODE: { + M5.Display.drawString("NORMAL MODE", 20, 40 + 35 * 5); + // motor channel 0 -3 NORMAL_MODE + for (uint8_t i = 0; i < 4; i++) { + driver.setMode(i, NORMAL_MODE); + driver.setMotorSpeed(i, 127); + } + break; + } + case POSITION_MODE: { + M5.Display.drawString("POSITION MODE", 20, 40 + 35 * 5); + // motor channel 0 -3 POSITION_MODE + + for (uint8_t i = 0; i < 4; i++) { + driver.setMode(i, POSITION_MODE); + driver.setEncoderValue(i, 0); + driver.setPostionPIDMaxSpeed(i, 127); + driver.setPositionPoint(i, 1000); + } + break; + } + case SPEED_MODE: { + M5.Display.drawString("SPEED MODE", 20, 40 + 35 * 5); + // motor channel 0 -3 SPEED_MODE + for (uint8_t i = 0; i < 4; i++) { + driver.setMode(i, SPEED_MODE); + driver.setSpeedPoint(i, 127); + } + break; + } + } + } +} diff --git a/library.json b/library.json index 5ba64722..d5e6afd3 100644 --- a/library.json +++ b/library.json @@ -10,7 +10,7 @@ "type": "git", "url": "https://github.com/m5stack/M5Core2.git" }, - "version": "0.1.8", + "version": "0.1.9", "frameworks": "arduino", "platforms": "espressif32", "headers": "M5Core2.h" diff --git a/library.properties b/library.properties index 8b95edd1..7e6bf367 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=M5Core2 -version=0.1.8 +version=0.1.9 author=M5Stack maintainer=M5Stack sentence=Library for M5Stack Core2 development kit diff --git a/src/AXP.cpp b/src/AXP.cpp index ab16dec7..28380390 100644 --- a/src/AXP.cpp +++ b/src/AXP.cpp @@ -118,7 +118,11 @@ void AXP::begin() { ina3221.begin(&Wire1); axp2101.set_lcd_back_light_voltage(3000); axp2101.set_lcd_and_tf_voltage(3300); - axp2101.set_bus_5v(true); + if (ina3221.getVoltage(INA3221_CH2) > 4.5f) + axp2101.set_bus_5v(false); + else { + axp2101.set_bus_5v(true); + } axp2101.set_sys_led(true); } else { _pmic = pmic_unknown; diff --git a/src/AXP192.cpp b/src/AXP192.cpp index 8143d164..d8a5b432 100644 --- a/src/AXP192.cpp +++ b/src/AXP192.cpp @@ -59,7 +59,7 @@ void AXP192::begin() { delay(100); // I2C_WriteByteDataAt(0X15,0XFE,0XFF); - // axp: check v-bus status & enable peripherals power (EXTEN) if 5V Out required + // axp: check v-bus status if (Read8bit(0x00) & 0x08) { Write1Byte(0x30, Read8bit(0x30) | 0x80); // if v-bus can use, disable M-Bus 5V output to input diff --git a/src/INA3221.cpp b/src/INA3221.cpp index 6c8a4984..553cac60 100644 --- a/src/INA3221.cpp +++ b/src/INA3221.cpp @@ -274,6 +274,8 @@ void INA3221::setChannelEnable(ina3221_ch_t channel) { case INA3221_CH3: conf_reg.ch3_en = 1; break; + default: + break; } _write(INA3221_REG_CONF, (uint16_t *)&conf_reg); @@ -294,14 +296,16 @@ void INA3221::setChannelDisable(ina3221_ch_t channel) { case INA3221_CH3: conf_reg.ch3_en = 0; break; + default: + break; } _write(INA3221_REG_CONF, (uint16_t *)&conf_reg); } void INA3221::setWarnAlertShuntLimit(ina3221_ch_t channel, int32_t voltageuV) { - ina3221_reg_t reg; - int16_t val = 0; + ina3221_reg_t reg = INA3221_REG_CONF; + int16_t val = 0; switch (channel) { case INA3221_CH1: @@ -313,6 +317,8 @@ void INA3221::setWarnAlertShuntLimit(ina3221_ch_t channel, int32_t voltageuV) { case INA3221_CH3: reg = INA3221_REG_CH3_WARNING_ALERT_LIM; break; + default: + break; } val = voltageuV / 5; @@ -320,8 +326,8 @@ void INA3221::setWarnAlertShuntLimit(ina3221_ch_t channel, int32_t voltageuV) { } void INA3221::setCritAlertShuntLimit(ina3221_ch_t channel, int32_t voltageuV) { - ina3221_reg_t reg; - int16_t val = 0; + ina3221_reg_t reg = INA3221_REG_CONF; + int16_t val = 0; switch (channel) { case INA3221_CH1: @@ -333,6 +339,8 @@ void INA3221::setCritAlertShuntLimit(ina3221_ch_t channel, int32_t voltageuV) { case INA3221_CH3: reg = INA3221_REG_CH3_CRIT_ALERT_LIM; break; + default: + break; } val = voltageuV / 5; @@ -368,6 +376,8 @@ void INA3221::setCurrentSumEnable(ina3221_ch_t channel) { case INA3221_CH3: masken_reg.shunt_sum_en_ch3 = 1; break; + default: + break; } _write(INA3221_REG_MASK_ENABLE, (uint16_t *)&masken_reg); @@ -389,6 +399,8 @@ void INA3221::setCurrentSumDisable(ina3221_ch_t channel) { case INA3221_CH3: masken_reg.shunt_sum_en_ch3 = 0; break; + default: + break; } _write(INA3221_REG_MASK_ENABLE, (uint16_t *)&masken_reg); @@ -397,8 +409,8 @@ void INA3221::setCurrentSumDisable(ina3221_ch_t channel) { int32_t INA3221::getShuntVoltage(ina3221_ch_t channel) { int32_t res; - ina3221_reg_t reg; - uint16_t val_raw = 0; + ina3221_reg_t reg = INA3221_REG_CONF; + uint16_t val_raw = 0; switch (channel) { case INA3221_CH1: @@ -410,6 +422,8 @@ int32_t INA3221::getShuntVoltage(ina3221_ch_t channel) { case INA3221_CH3: reg = INA3221_REG_CH3_SHUNTV; break; + default: + break; } _read(reg, &val_raw); @@ -494,9 +508,9 @@ float INA3221::getCurrentCompensated(ina3221_ch_t channel) { } float INA3221::getVoltage(ina3221_ch_t channel) { - float voltage_V = 0.0; - ina3221_reg_t reg; - uint16_t val_raw = 0; + float voltage_V = 0.0; + ina3221_reg_t reg = INA3221_REG_CONF; + uint16_t val_raw = 0; switch (channel) { case INA3221_CH1: @@ -508,6 +522,8 @@ float INA3221::getVoltage(ina3221_ch_t channel) { case INA3221_CH3: reg = INA3221_REG_CH3_BUSV; break; + default: + break; } _read(reg, &val_raw);