yahboom
HomeLearn
Solution
Blog
Online Store
About Us
EN/中文
310 Motor📖
1.Hardware parameter
  • Hardware parameter
2.ROS robot control board tutorials
  • 3.0 ReadMe
  • 3.1 Control motor
  • 3.2 Timer got encoder data
  • 3.3 PID control robot movement
  • 3.4 4WD Robot kinematics analysis
  • 3.5 Ackerman Robot kinematics analysis
  • 3.6 Mecanum wheel kinematics analysis
3.STM32 smart car tutorials
0. Get started quickly
  • 14.Infrared sensor obstacle avoidance
  • 0. Installation Instructions
  • 1. Version configuration
  • 2. Smart car installation video
  • 3. Precautions for use
1. Introduction to STM32 board
  • 1. Learn about STM32
  • 2. Introduction to STM32 development board
  • 3. Development board pin assignment
  • 4. Introduction to HAL library and LL library
2. Development environment
  • 1. Common development tools
  • 2. STM32CubeMX Install - Use
  • 3. STM32CubeIDE Install - Use
  • 4. Common Driver Installation
  • 5. Program download and simulation
  • 6. Engineering experience and migration
3. Car expansion tutorial
  • 1.Servo control
  • 2.Basic use ultrasonic servo PTZ
  • 3.Motor control
  • 4.OLED display light sensitivity value
  • 5.Infrared sensor
  • 6.Four-way tracking module
  • 7.Infrared remote control
  • 8.Ultrasonic ranging
  • 9.PS2 controller test
  • 10.Car line patrol
  • 11.Ultrasonic follow
  • 12.Ultrasonic obstacle avoidance
  • 13.Ultrasonic obstacle avoidance (servo PTZ version)
  • 15.Infrared remote control
  • 16.PS2 handle remote control
4.2-channel motor drive module tutorial
  • 2.1 Introduction of Module
  • 2.2 For STM32
  • 2.3 For Arduino
  • 2.4 For RaspberryPi
  • 2.5 For RaspberryPi Pico
  • 2.6 For jetson nano
  • 2.7 For MSPM0G3507
  • 2.8 For MSPM0G3519new
5.4-channel motor drive tutorials
Introduction and debugging
  • 0.Motor introduction and usage
  • 1.Introduction to 4-channel motor drive board
  • 2.Control command
  • 3.PC host
USART communication
  • For Arduino
  • For Jetson NANO
  • For MSPM0G3507
  • For MSPM0G3519new
  • For PICO2
  • For RDK X5
  • For RPi5
  • For STM32
IIC communication
  • For Arduino
  • For ESP32
  • For ESP32
  • For Jetson NANO
  • For MSPM0G3507
  • For MSPM0G3519new
  • For PICO2
  • For RDK X5
  • For RPi5
  • For STM32
Download
  • Motor_3D_Model_File
  • Annex_ROSRobotControlBoard
  • Annex_STM3RobotCar
  • Annex_2-channelMotorDriveModule
  • Annex_4-channelMotorDriveModule
Download ZIP

Welcome to 310 Motor repository

For MSPM0G3519

Control car and read encoder-IIC

1.1 Explanation

Please read 《0. Motor introduction and usage》first to understand the motor parameters, wiring method, and power supply voltage you are currently using. To avoid improper operation and damage to the driver board or motor.

I2C and serial communication cannot be shared, only one can be selected.

The 4 motor interfaces on the module correspond to motors on robot car, as shown below:

M1 -> Upper left motor (left front wheel of the car) M2 -> Lower left motor (left rear wheel of the car) M3 -> Upper right motor (right front wheel of the car) M4 -> Lower right motor (right rear wheel of the car)

Hardware wiring:

4-channel motor drive boardMSPM0G3519
SDAPA13
SCLPA12
GNDGND
5V5V
Motor4-channel motor drive board(Motor)
M2M-
V3V3
AH1B
BH1A
GGND
M1M+

The USB to TTL serial port module is connected here to print the processed encoder data. If you are using the MSPM0G3519 purchased from our store, you can directly connect the TYPE-C interface to the computer USB port, and you can also use serial port printing, so you do not need to connect a USB to TTL serial port module.

USB TO TTLMSPM0G3519
VCC3V3
GNDGND
RXDPA10
TXDPA11

Serial port configuration: Baud rate 115200, no parity check, no hardware flow control, 1 stop bit

Note: The serial port here is used to print data on the serial port assistant, not for communication with the driver board

1.2 Code analysis

​x
#define UPLOAD_DATA 2  // 1: Receive total encoder data 2: Receive real-time encoder data

#define MOTOR_TYPE 1   //1:520 motor 2:310 motor 3:speed code disc TT motor 4:TT DC reduction motor 5:L type 520 motor
  • UPLOAD_DATA: used to set the data of the motor encoder. Set 1 to the total number of encoder pulses and 2 to the real-time pulse data of 10ms.

  • MOTOR_TYPE: used to set the type of motor used. Just modify the corresponding numbers according to the comments according to the motor you are currently using. You don’t need to modify the rest of the code.

If you need to drive the motor and observe the data, just modify the two numbers at the beginning of the program. No changes are required to the rest of the code.

xxxxxxxxxx
    #if MOTOR_TYPE == 1
    Set_motor_type(1);//Configure motor type
    delay_ms(100);
    Set_Pluse_Phase(30);//Configure the reduction ratio. Check the motor manual to find out
    delay_ms(100);
    Set_Pluse_line(11);//Configure the magnetic ring wire. Check the motor manual to get the result.
    delay_ms(100);
    Set_Wheel_dis(67.00);//Configure the wheel diameter and measure it
    delay_ms(100);
    Set_motor_deadzone(1900);//Configure the motor dead zone, and the experiment shows
    delay_ms(100);
    
    #elif MOTOR_TYPE == 2
    Set_motor_type(2);
    delay_ms(100);
    Set_Pluse_Phase(20);
    delay_ms(100);
    Set_Pluse_line(13);
    delay_ms(100);
    Set_Wheel_dis(48.00);
    delay_ms(100);
    Set_motor_deadzone(1600);
    delay_ms(100);
    
    #elif MOTOR_TYPE == 3
    Set_motor_type(3);
    delay_ms(100);
    Set_Pluse_Phase(45);
    delay_ms(100);
    Set_Pluse_line(13);
    delay_ms(100);
    Set_Wheel_dis(68.00);
    delay_ms(100);
    Set_motor_deadzone(1250);
    delay_ms(100);
    
    #elif MOTOR_TYPE == 4
    Set_motor_type(4);
    delay_ms(100);
    Set_Pluse_Phase(48);
    delay_ms(100);
    Set_motor_deadzone(1000);
    delay_ms(100);
    
    #elif MOTOR_TYPE == 5
    Set_motor_type(1);
    delay_ms(100);
    Set_Pluse_Phase(40);
    delay_ms(100);
    Set_Pluse_line(11);
    delay_ms(100);
    Set_Wheel_dis(67.00);
    delay_ms(100);
    Set_motor_deadzone(1900);
    delay_ms(100);
    #endif

This is used to store the parameters of the motors sold in our store. By modifying the MOTOR_TYPE parameter above, one-click configuration can be achieved. Normally, when using the motor of our store, do not modify the code here. If you are using your own motor, or if a certain data needs to be modified according to your needs, you can check the document 《4-channel motor drive board control commands》 to understand the specific meaning of each command.

xxxxxxxxxx
    while(1)
    {
        if(times>=25)//The timer accumulates every 100ms, and when it reaches 25 times, that is, every 2.5 seconds, the state of the car is changed.
        {
            #if MOTOR_TYPE == 4
            Car_Move_PWM();
            #else
            Car_Move();
            #endif
            times = 0;
        }
        #if UPLOAD_DATA == 1
        Read_ALL_Enconder();
        printf("M1:%d\t M2:%d\t M3:%d\t M4:%d\t \r\n",Encoder_Now[0],Encoder_Now[1],Encoder_Now[2],Encoder_Now[3]);
        #elif UPLOAD_DATA == 2
        Read_10_Enconder();
        printf("M1:%d\t M2:%d\t M3:%d\t M4:%d\t \r\n",Encoder_Offset[0],Encoder_Offset[1],Encoder_Offset[2],Encoder_Offset[3]);
        #endif
    }
    
}

//The timer's interrupt service function interrupts once every 100ms
void TIMER_0_INST_IRQHandler(void)
{
    //If a timer interrupt occurs
    switch( DL_TimerG_getPendingInterrupt(TIMER_0_INST) )
    {
        case DL_TIMER_IIDX_ZERO://If it is 0 overflow interrupt
            times++;
            break;

        default:
            break;
    }
}

A 100ms timer is set in the program. Each time the timer interrupt is triggered, the times variable will be incremented by one. When it reaches 25 times, that is, 2.5 seconds, the motion state of the car will be changed. If the motor type is 4, that is, the motor without encoder, then the pwm version of the car state switching function is used. At the same time, read the data sent by the driver board and print the data out at the same time.

xxxxxxxxxx
//Read the data of the encoder of relative time 10ms
void Read_10_Enconder(void)
{
    static int8_t buf[2];
        
    //M1 motor encoder data
    i2cRead(Motor_model_ADDR, READ_TEN_M1Enconer_REG, 2, buf);
    Encoder_Offset[0] = buf[0]<<8|buf[1]; 
    
    //M2 motor encoder data
    i2cRead(Motor_model_ADDR, READ_TEN_M2Enconer_REG, 2, buf);
    Encoder_Offset[1] = buf[0]<<8|buf[1];
    
    //M3 motor encoder data
    i2cRead(Motor_model_ADDR, READ_TEN_M3Enconer_REG, 2, buf);
    Encoder_Offset[2] = buf[0]<<8|buf[1];
    
    //M4 motor encoder data
    i2cRead(Motor_model_ADDR, READ_TEN_M4Enconer_REG, 2, buf);
    Encoder_Offset[3] = buf[0]<<8|buf[1];
    
}

//Read the encoder data of the motor rotation
void Read_ALL_Enconder(void)
{
    static uint8_t buf[2];
    static uint8_t buf2[2];
    
    //M1 motor encoder data
    i2cRead(Motor_model_ADDR, READ_ALLHigh_M1_REG, 2, buf);
    i2cRead(Motor_model_ADDR, READ_ALLLOW_M1_REG, 2, buf2);
    
    Encoder_Now[0] = buf[0]<<24|buf[1]<<16|buf2[0]<<8|buf2[1]; 
    
    //M2 motor encoder data
    i2cRead(Motor_model_ADDR, READ_ALLHigh_M2_REG, 2, buf);
    i2cRead(Motor_model_ADDR, READ_ALLLOW_M2_REG, 2, buf2);
    Encoder_Now[1] = buf[0]<<24|buf[1]<<16|buf2[0]<<8|buf2[1];
    
    //M3 motor encoder data
    i2cRead(Motor_model_ADDR, READ_ALLHigh_M3_REG, 2, buf);
    i2cRead(Motor_model_ADDR, READ_ALLLOW_M3_REG, 2, buf2);
    Encoder_Now[2] = buf[0]<<24|buf[1]<<16|buf2[0]<<8|buf2[1];
    
    //M4 motor encoder data
    i2cRead(Motor_model_ADDR, READ_ALLHigh_M4_REG, 2, buf);
    i2cRead(Motor_model_ADDR, READ_ALLLOW_M4_REG, 2, buf2);
    Encoder_Now[3] = buf[0]<<24|buf[1]<<16|buf2[0]<<8|buf2[1];
    
}

After getting the data from the driver board, it is shifted, and the shift is required to get the correct data.

1.3 Experimental phenomenon

After the wiring is correct, write the program into the mainboard. After resetting, you can see that the car will move forward for 2.5S, move backward for 2.5S, rotate right for 2.5S, rotate left for 2.5S, then stop for 2.5S, and continue the above state switching actions. At the same time, you can see that the printed motor values are constantly changing in the serial port assistant.

For MSPM0G3507
For PICO2

COMPANY

  • About us
  • Team Style
  • Blog

LEARNING & EXPLORATION

  • Tutorials
  • Edge AI hardware
  • Educational robots
  • Embedded intelligent hardware

WHERE TO BUY

  • Yahboom Online Store
  • Amazon
  • Alibaba
  • AliExpress
  • Mercado

TECHNOLOGY SUPPORT

  • Email: support@yahboom.com

BE A DISTRIBUTOR

  • Email: sales@yahboom.com
QR Code
QR Code

Copyright © 2017-2026 Yahboom. All Rights Reserved.

EN/中文