Skip to content

Commit

Permalink
Add copyright and dependency information(Module)
Browse files Browse the repository at this point in the history
  • Loading branch information
renletao committed Jan 16, 2025
1 parent d375924 commit 60cba67
Show file tree
Hide file tree
Showing 63 changed files with 1,758 additions and 1,391 deletions.
39 changes: 18 additions & 21 deletions examples/Modules/4EncoderMotor/4EncoderMotor.ino
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
/**
* @file DriverSample.ino
* @author SeanKwok ([email protected])
* @brief Module 4EncoderMotor Test Demo.
* @version 0.1
* @date 2024-01-19
*
/*
* SPDX-FileCopyrightText: 2025 M5Stack Technology CO LTD
*
* SPDX-License-Identifier: MIT
*/
/*
* @Hardwares: M5Core + Module 4EncoderMotor
* @Platform Version: Arduino M5Stack Board Manager v2.1.0
* @Platform Version: Arduino M5Stack Board Manager v2.1.3
* @Dependent Library:
* M5Unified: https://github.com/m5stack/M5Unified
* M5GFX: https://github.com/m5stack/M5GFX
* M5GFX@^0.2.3: https://github.com/m5stack/M5GFX
* M5Unified@^0.2.2: https://github.com/m5stack/M5Unified
* M5Module4EncoderMotor: https://github.com/m5stack/M5Module-4EncoderMotor
*/

Expand All @@ -20,15 +18,16 @@

M5Module4EncoderMotor driver;

#define MAX_RECORD_SIZE 256
#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 avg_filter(float *data, int len)
{
float sum = 0;
float min = data[0];
float max = data[0];
Expand All @@ -46,7 +45,8 @@ float avg_filter(float *data, int len) {
return sum / (len - 2);
}

void setup() {
void setup()
{
M5.begin();
M5.Display.begin();

Expand Down Expand Up @@ -78,13 +78,13 @@ void setup() {
bool direction = true;
int mode = NORMAL_MODE;

void loop() {
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);
M5.Display.drawString("CH" + String(i) + ": " + String(encoder_value), 20, 40 + 35 * i);
}

if (avg_filter_level != 0) {
Expand All @@ -100,12 +100,9 @@ void loop() {
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);
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())) {
if (M5.BtnA.wasClicked() || (M5.Touch.getCount() && M5.Touch.getDetail(0).wasClicked())) {
mode++;
if (mode > SPEED_MODE) {
mode = NORMAL_MODE;
Expand Down
47 changes: 25 additions & 22 deletions examples/Modules/AC-SOCKET/AC-SOCKET.ino
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
/*
*******************************************************************************
* Copyright (c) 2023 by M5Stack
* Equipped with M5Core sample source code
* 配套 M5Core 示例源代码
* Visit for more information: https://docs.m5stack.com/en/products
* 获取更多资料请访问: https://docs.m5stack.com/zh_CN/products
*
* Describe: AC Socket.
* Date: 2021/9/1
*******************************************************************************
Click button B to turn on the power. Click button A to turn off the power.
单击按钮 B 打开电源。 单击按钮 A 关闭电源。
*/
* SPDX-FileCopyrightText: 2025 M5Stack Technology CO LTD
*
* SPDX-License-Identifier: MIT
*/
/*
* @Hardwares: M5Core + AC-Socket
* @Platform Version: Arduino M5Stack Board Manager v2.1.3
* @Dependent Library:
* M5Stack@^0.4.6: https://github.com/m5stack/M5Stack
*/

#include <Arduino.h>
#include <M5Stack.h>

#include "modbus.h"
#include "protocol.h"

volatile uint32_t tim = 0;
// Click button B to turn on the power. Click button A to turn off the power.
// 单击按钮 B 打开电源。 单击按钮 A 关闭电源。

uint32_t time_now = 0;
uint8_t ucTestFlag = 0;
bool ubCoilState = false;
volatile uint32_t tim = 0;
uint32_t time_now = 0;
uint8_t ucTestFlag = 0;
bool ubCoilState = false;

void setup() {
void setup()
{
M5.begin(true, false, true, false);
M5.Power.begin();
mb_init(0xac, 9600, &tim);
Expand All @@ -33,7 +33,8 @@ void setup() {
// put your setup code here, to run once:
}

void loop() {
void loop()
{
M5.update();

if (millis() - time_now > 60000UL) {
Expand Down Expand Up @@ -73,10 +74,12 @@ void loop() {
// put your main code here, to run repeatedly:
}

void mb_send_one_byte(uint8_t data) {
void mb_send_one_byte(uint8_t data)
{
Serial1.write(data);
}

void protocol_callback(CmdData cmd) {
void protocol_callback(CmdData cmd)
{
Serial.printf("got ... \r\n");
}
46 changes: 28 additions & 18 deletions examples/Modules/BALA/BALA.ino
Original file line number Diff line number Diff line change
@@ -1,25 +1,35 @@
/*
Description: This code only for mpu6886! first init need press BtnC and
reboot to calibrate !
*/
* SPDX-FileCopyrightText: 2025 M5Stack Technology CO LTD
*
* SPDX-License-Identifier: MIT
*/
/*
* @Hardwares: M5Core + Bala
* @Platform Version: Arduino M5Stack Board Manager v2.1.3
* @Dependent Library:
* M5Stack@^0.4.6: https://github.com/m5stack/M5Stack
*/

#include <M5Stack.h>
#include <Preferences.h>
#include <Wire.h>

#include "M5Bala.h"
#include "imuCalibration.h"

Preferences preferences;

M5Bala m5bala(Wire);
// Description: This code only for mpu6886! first init need press BtnC and reboot to calibrate !

// ================ Draw Angle Wavefrom =================
void draw_waveform() {
#define MAX_LEN 120
#define X_OFFSET 0
#define Y_OFFSET 100
#define X_SCALE 3

Preferences preferences;

M5Bala m5bala(Wire);

// ================ Draw Angle Wavefrom =================
void draw_waveform()
{
static int16_t val_buf[MAX_LEN] = {0};
static int16_t pt = MAX_LEN - 1;

Expand All @@ -33,14 +43,14 @@ void draw_waveform() {
M5.Lcd.drawLine(i, val_buf[(now_pt + 1) % MAX_LEN] + Y_OFFSET, i + 1,
val_buf[(now_pt + 2) % MAX_LEN] + Y_OFFSET, TFT_BLACK);
if (i < MAX_LEN - 1)
M5.Lcd.drawLine(i, val_buf[now_pt] + Y_OFFSET, i + 1,
val_buf[(now_pt + 1) % MAX_LEN] + Y_OFFSET,
M5.Lcd.drawLine(i, val_buf[now_pt] + Y_OFFSET, i + 1, val_buf[(now_pt + 1) % MAX_LEN] + Y_OFFSET,
TFT_GREEN);
}
}

// ================ GYRO offset param ==================
void auto_tune_gyro_offset() {
void auto_tune_gyro_offset()
{
M5.Speaker.tone(500, 200);
delay(300);
M5.update();
Expand All @@ -63,7 +73,8 @@ void auto_tune_gyro_offset() {
preferences.end();
}

void setup() {
void setup()
{
// Power ON Stabilizing...
M5.begin();
M5.Power.begin();
Expand All @@ -89,16 +100,15 @@ void setup() {
}
}

void loop() {
void loop()
{
// LCD display
static uint32_t print_interval = millis() + 30;
if (millis() > print_interval) {
print_interval = millis() + 100;
M5.Lcd.setCursor(0, 190);
M5.Lcd.printf("Input Encoer0: %+4d Encoer1: %+4d \r\n",
m5bala.getSpeed0(), m5bala.getSpeed1());
M5.Lcd.printf("Output PWM0: %+4d PWM1: %+4d \r\n",
m5bala.getOut0(), m5bala.getOut1());
M5.Lcd.printf("Input Encoer0: %+4d Encoer1: %+4d \r\n", m5bala.getSpeed0(), m5bala.getSpeed1());
M5.Lcd.printf("Output PWM0: %+4d PWM1: %+4d \r\n", m5bala.getOut0(), m5bala.getOut1());
M5.Lcd.printf("AngleX: %+05.2f\r\n", m5bala.getAngle());
}

Expand Down
49 changes: 29 additions & 20 deletions examples/Modules/BALA2/BALA2.ino
Original file line number Diff line number Diff line change
@@ -1,22 +1,31 @@
/*
Description: BALA2 stand and run in balance.
Note: click the device power button and Long press button B will setup
calibration mode. at calibration mode Press the A/C key to increase or
decrease the correction value. When it is adjusted to a satisfactory value,
press the B key to save the parameter.
*/
* SPDX-FileCopyrightText: 2025 M5Stack Technology CO LTD
*
* SPDX-License-Identifier: MIT
*/
/*
* @Hardwares: M5Core + Bala2
* @Platform Version: Arduino M5Stack Board Manager v2.1.3
* @Dependent Library:
* M5Stack@^0.4.6: https://github.com/m5stack/M5Stack
*/

#define M5STACK_MPU6886

#include <M5Stack.h>

#include "MadgwickAHRS.h"
#include "bala.h"
#include "calibration.h"
#include "freertos/FreeRTOS.h"
#include "imu_filter.h"
#include "pid.h"

// Description: BALA2 stand and run in balance.
// Note: click the device power button and Long press button B will setup
// calibration mode. at calibration mode Press the A/C key to increase or
// decrease the correction value. When it is adjusted to a satisfactory value,
// press the B key to save the parameter.

extern uint8_t bala_img[41056];
static void PIDTask(void *arg);
static void draw_waveform();
Expand All @@ -34,7 +43,8 @@ PID pid(angle_point, kp, ki, kd);
PID speed_pid(0, s_kp, s_ki, s_kd);

// the setup routine runs once when M5Stack starts up
void setup() {
void setup()
{
// Initialize the M5Stack object

M5.begin(true, false, false, false);
Expand All @@ -52,8 +62,7 @@ void setup() {
}

calibrationGet(&x_offset, &y_offset, &z_offset, &angle_center);
Serial.printf("x: %d, y: %d, z:%d, angle: %.2f", x_offset, y_offset,
z_offset, angle_center);
Serial.printf("x: %d, y: %d, z:%d, angle: %.2f", x_offset, y_offset, z_offset, angle_center);

angle_point = angle_center;
pid.SetPoint(angle_point);
Expand All @@ -73,7 +82,8 @@ void setup() {
}

// the loop routine runs over and over again forever
void loop() {
void loop()
{
static uint32_t next_show_time = 0;
vTaskDelay(pdMS_TO_TICKS(5));

Expand All @@ -100,7 +110,8 @@ void loop() {
}
}

static void PIDTask(void *arg) {
static void PIDTask(void *arg)
{
float bala_angle;
float motor_speed = 0;

Expand Down Expand Up @@ -153,29 +164,27 @@ static void PIDTask(void *arg) {
}
}

static void draw_waveform() {
static void draw_waveform()
{
#define MAX_LEN 120
#define X_OFFSET 100
#define Y_OFFSET 95
#define X_SCALE 3
static int16_t val_buf[MAX_LEN] = {0};
static int16_t pt = MAX_LEN - 1;
val_buf[pt] = constrain((int16_t)(getAngle() * X_SCALE), -50, 50);
val_buf[pt] = constrain((int16_t)(getAngle() * X_SCALE), -50, 50);

if (--pt < 0) {
pt = MAX_LEN - 1;
}

for (int i = 1; i < (MAX_LEN); i++) {
uint16_t now_pt = (pt + i) % (MAX_LEN);
M5.Lcd.drawLine(i + X_OFFSET,
val_buf[(now_pt + 1) % MAX_LEN] + Y_OFFSET,
i + 1 + X_OFFSET,
M5.Lcd.drawLine(i + X_OFFSET, val_buf[(now_pt + 1) % MAX_LEN] + Y_OFFSET, i + 1 + X_OFFSET,
val_buf[(now_pt + 2) % MAX_LEN] + Y_OFFSET, TFT_BLACK);
if (i < MAX_LEN - 1) {
M5.Lcd.drawLine(
i + X_OFFSET, val_buf[now_pt] + Y_OFFSET, i + 1 + X_OFFSET,
val_buf[(now_pt + 1) % MAX_LEN] + Y_OFFSET, TFT_GREEN);
M5.Lcd.drawLine(i + X_OFFSET, val_buf[now_pt] + Y_OFFSET, i + 1 + X_OFFSET,
val_buf[(now_pt + 1) % MAX_LEN] + Y_OFFSET, TFT_GREEN);
}
}
}
Loading

0 comments on commit 60cba67

Please sign in to comment.