Skip to content

Commit

Permalink
Merge branch 'master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
wswag authored Mar 25, 2024
2 parents 507e272 + c26f1b6 commit 7f9626d
Show file tree
Hide file tree
Showing 9 changed files with 191 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -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)

Expand Down
2 changes: 1 addition & 1 deletion README_cn.md
Original file line number Diff line number Diff line change
@@ -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)

Expand Down
148 changes: 148 additions & 0 deletions examples/Module/4EncoderMotor/4EncoderMotor.ino
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
}
2 changes: 1 addition & 1 deletion library.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -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
Expand Down
6 changes: 5 additions & 1 deletion src/AXP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/AXP192.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
34 changes: 25 additions & 9 deletions src/INA3221.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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:
Expand All @@ -313,15 +317,17 @@ 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;
_write(reg, (uint16_t *)&val);
}

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:
Expand All @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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:
Expand All @@ -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);
Expand Down Expand Up @@ -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:
Expand All @@ -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);
Expand Down

0 comments on commit 7f9626d

Please sign in to comment.