Motor BLDC
1 Introduction
This project is to design Brushless DC motor control on RT-Thread.
2. Build the IDE environment
Make the rt-thread extension installed in VS code on BSP file in rt-thread directory,
Configure the peripheral in rt-configuration, run
menuconfig
Select GPIO
And activate PWM drivers
Then, update the packages with
Pkgs --upgrade
Run and build the program with
scons
Plug the board with USB-B wire to desktop computer,
Drag and release the rtthread.bin binary file to the board,
Then start the serial terminal Putty in 115200-8-1-N
The output shows the rtthread works OK.
3 The BLDC motor driver
3.1 Connect the BLDC motor driver board with RT-1060 EVKB
and the 24V BLDC motor
As hall sensor in arduino A0, A1, A2
Voltage ADC in A3, A4, A5
MOSFET bridge control as follows
// UP_PIN //D3
// VP_PIN //D5
// WP_PIN //D6
// UN_PIN //D7
// VN_PIN //D9
// WN_PIN //D11
3.2 Here is the main.c code
#include <rtthread.h>
#include "foc.h"
#define THREAD_PRIORITY 25
#define THREAD_STACK_SIZE 512
#define THREAD_TIMESLICE 5
#define START 0
#define STOP 1
#define IDLE 2
rt_uint32_t state;
static rt_thread_t stateMachine = RT_NULL;
static void stateMachine_entry(void *parameter)
{
rt_uint32_t count = 0;
count ++;
switch (state)
{
case START:
{
motorMode(START);
break;
}
case STOP:
{
motorMode(STOP);
break;
}
case IDLE:
{
motorMode(IDLE);
break;
}
default:
break;
}
//while (1) { rt_kprintf("stateMachine_entry count: %d\n", count ); rt_thread_mdelay(2000); }
}
int main(void)
{
foc_init();
stateMachine = rt_thread_create("statMachine",
stateMachine_entry, RT_NULL,
THREAD_STACK_SIZE,
THREAD_PRIORITY, THREAD_TIMESLICE);
if (stateMachine != RT_NULL)
rt_thread_startup(stateMachine);
}
void commd(int argc, char**argv)
{
//rt_kprintf("Motor Command init. ");
rt_kprintf( argv[1]);
if (!rt_strcmp(argv[1], "start"))
{
rt_kprintf("Motor start.\n");
}
else if (!rt_strcmp(argv[1], "stop"))
{
rt_kprintf("Motor stop.\n");
}
else
{
rt_kprintf("Motor idle.\n");
}
}
MSH_CMD_EXPORT(commd, commd m 1)
void reboot(void)
{
NVIC_SystemReset();
}
MSH_CMD_EXPORT(reboot, reset system)
3.3 Explanation to the code
The core of the core is stateMachine thread to control the motor mode, then drive the UVW accordingly with 6 PWM channels.
With the source code as attached in zip format.
Comments