Because of the particularity of mcnamur wheel , Each wheel needs a motor for independent control . The installation sequence of wheels is ABAB( The order in the comment is :B round A round D round C round ), How to install a lot of online information , Driver recommended L298N Four way drive module . Don't say much , Go straight to the program :
Mcnamm wheel installation direction A round \\ ------ // B round \\ ------ // ------ ------ ------ // ------ \\ D round
// ------ \\ C round // Four wheel drive chassis and four wheel mcnam wheel chassis // Hardware connection description : timer8 control ABCD Four wheeled PWM TIM8_CH1--PC7--
A round // Left front ---- motor drive 1-ENA TIM8_CH2--PC6--B round // Right front ---- motor drive 1-ENB TIM8_CH3--PC8--C round
// Right back ---- motor drive 2-ENA TIM8_CH4--PC9--D round // Left rear ---- motor drive 2-ENB A round :PC1 PC0 Control forward and backward movement PC1
----IN2,PC0----IN1 B round :PC3 PC2 Control forward and backward movement PC3----IN4,PC2----IN3 C round :PC10,PC11 Control forward and backward movement
PC10--- IN1,PC11---IN2 D round :PC12,PD2 Control forward and backward movement PC12 --- IN3,PD2 ---IN4 ***************
********************************************************************************
********** */ #include "motor.h"
//*************************** Equipped with motor drive IO mouth ***************************// void
MOTOR_GPIO_Init(void) { /* Define a GPIO_InitTypeDef Structure of type */ GPIO_InitTypeDef
GPIO_InitStructure; RCC_APB2PeriphClockCmd(MOTOR_CLK, ENABLE); /* open GPIO Peripheral clock of */
GPIO_InitStructure.GPIO_Pin = MOTOR_A1 | MOTOR_A2 | MOTOR_B1 | MOTOR_B2 |
MOTOR_C1| MOTOR_C2 | MOTOR_D1; /* Select the object to control GPIO Pin */ GPIO_InitStructure.GPIO_Mode =
GPIO_Mode_Out_PP; /* Set pin mode to universal push pull output */ GPIO_InitStructure.GPIO_Speed =
GPIO_Speed_50MHz; /* Set pin speed to 50MHz */ GPIO_Init(MOTOR_PORT, &GPIO_InitStructure);
/* Calling library functions , initialization GPIO*/ RCC_APB2PeriphClockCmd( MOTOR_CLK2, ENABLE); /* open GPIO Peripheral clock of */
GPIO_InitStructure.GPIO_Pin = MOTOR_D2; /* Select the object to control GPIO Pin */ GPIO_InitStructure.
GPIO_Mode= GPIO_Mode_Out_PP; /* Set pin mode to universal push pull output */ GPIO_InitStructure.GPIO_Speed =
GPIO_Speed_50MHz; /* Set pin speed to 50MHz */ GPIO_Init(MOTOR_PORT2, &GPIO_InitStructure);
/* Calling library functions , initialization GPIO*/ } // Left front A electric machinery void MOTOR_A(char state) { if(state == GO)// Left front motor forward {
GPIO_SetBits(MOTOR_PORT,MOTOR_A2); GPIO_ResetBits(MOTOR_PORT,MOTOR_A1); } if(
state== BACK)// Left front motor backward { GPIO_SetBits(MOTOR_PORT,MOTOR_A1); GPIO_ResetBits(
MOTOR_PORT,MOTOR_A2); } if(state == STOP)// Stall { GPIO_ResetBits(MOTOR_PORT,
MOTOR_A1); GPIO_ResetBits(MOTOR_PORT,MOTOR_A2); } } // Right front B electric machinery void MOTOR_B(char
state) { if(state == GO)// Right front motor forward { GPIO_SetBits(MOTOR_PORT,MOTOR_B1);
GPIO_ResetBits(MOTOR_PORT,MOTOR_B2); } if(state == BACK)// Right front motor backward { GPIO_SetBits(
MOTOR_PORT,MOTOR_B2); GPIO_ResetBits(MOTOR_PORT,MOTOR_B1); } if(state == STOP)
// Stall { GPIO_ResetBits(MOTOR_PORT,MOTOR_B1); GPIO_ResetBits(MOTOR_PORT,MOTOR_B2);
} } // Left rear C electric machinery void MOTOR_C(char state) { if(state == GO)// Rear left motor forward { GPIO_SetBits(
MOTOR_PORT,MOTOR_C2); GPIO_ResetBits(MOTOR_PORT,MOTOR_C1); } if(state == BACK)
// Left rear motor backward { GPIO_SetBits(MOTOR_PORT,MOTOR_C1); GPIO_ResetBits(MOTOR_PORT,MOTOR_C2
); } if(state == STOP)// Stall { GPIO_ResetBits(MOTOR_PORT,MOTOR_C1); GPIO_ResetBits
(MOTOR_PORT,MOTOR_C2); } } // Right back D electric machinery void MOTOR_D(char state) { if(state == GO)
// Right rear motor forward { GPIO_SetBits(MOTOR_PORT2,MOTOR_D2); GPIO_ResetBits(MOTOR_PORT,
MOTOR_D1); } if(state == BACK)// Right rear motor backward { GPIO_SetBits(MOTOR_PORT,MOTOR_D1);
GPIO_ResetBits(MOTOR_PORT2,MOTOR_D2); } if(state == STOP)// Stall { GPIO_ResetBits(
MOTOR_PORT,MOTOR_D1); GPIO_ResetBits(MOTOR_PORT2,MOTOR_D2); } }
//*************************** forward ***************************//
// Just configure it INx() The state of the motor can change the direction of rotation void Car_Go(void) { // Left front motor front // Right front motor front MOTOR_A(GO);
MOTOR_B(GO); // Rear left motor front // Right rear motor front MOTOR_D(GO); MOTOR_C(GO); } void Car_Back(void) {
// Left front motor after // Right front motor after MOTOR_A(BACK); MOTOR_B(BACK); // Rear left motor after // Right rear motor after MOTOR_D(BACK);
MOTOR_C(BACK); } //*************************** towards the left ***************************//
void Car_Left(void) { // Left front motor after // Right front motor front MOTOR_A(BACK); MOTOR_B(GO); // Rear left motor front
// Right rear motor after MOTOR_D(GO); MOTOR_C(BACK); }
//*************************** Left forward 45 degree ***************************// void
Car_LeftQ45(void) { // Left front motor stop // Right front motor front MOTOR_A(STOP); MOTOR_B(GO); // Rear left motor front
// Right rear motor stop MOTOR_D(GO); MOTOR_C(STOP); }
//*************************** Left back 45 degree ***************************// void
Car_LeftH45(void) { // Left front motor after // Right front motor stop MOTOR_A(BACK); MOTOR_B(STOP); // Rear left motor stop
// Right rear motor after MOTOR_D(STOP); MOTOR_C(BACK); }
//*************************** Turn left ***************************// void
Car_Turn_Left(void) { // Left front motor after // Right front motor front MOTOR_A(BACK); MOTOR_B(GO); // Rear left motor after
// Right rear motor front MOTOR_D(BACK); MOTOR_C(GO); }
//*************************** towards the right ***************************// void Car_Right(void
) { // Left front motor front // Right front motor after MOTOR_A(GO); MOTOR_B(BACK); // Rear left motor after // Right rear motor front MOTOR_D(BACK
); MOTOR_C(GO); }
//*************************** Right forward 45 degree ***************************// void
Car_RightQ45(void) { // Left front motor front // Right front motor stop MOTOR_A(GO); MOTOR_B(STOP); // Rear left motor stop
// Right rear motor front MOTOR_D(STOP); MOTOR_C(GO); }
//*************************** Right back 45 degree ***************************// void
Car_RightH45(void) { // Left front motor stop // Right front motor after MOTOR_A(STOP); MOTOR_B(BACK); // Rear left motor after
// Right rear motor stop MOTOR_D(BACK); MOTOR_C(STOP); }
//*************************** Turn right ***************************// void
Car_Turn_Right(void) { // Left front motor front // Right front motor after MOTOR_A(GO); MOTOR_B(BACK); // Rear left motor front
// Right rear motor after MOTOR_D(GO); MOTOR_C(BACK); }
//*************************** parking ***************************// void Car_Stop(void)
{ // Left front motor stop // Right front motor stop MOTOR_A(STOP); MOTOR_B(STOP); // Rear left motor stop // Right rear motor stop MOTOR_D(STOP
); MOTOR_C(STOP);
The above is the function construction of several kinds of car motion , The next step is to use the four-way tracking module to complete the tracking task program code :
#include "find.h" #include "bsp_timer8.h" #include "bsp_sys.h" #include
"delay.h" // Follow the trail IO initialization // Tracking sensor from right to left O1 O2 O3 O4 // Hardware connection O1-PA4,O2-PA5,O3-PA6,O4-PA7,
// To initialize to input mode void Find_IO_Init(void) { GPIO_InitTypeDef GPIO_InitStructure;
// open GPIO Clock RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5| GPIO_Pin_6 | GPIO_Pin_7;
// choice IO port GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;// Configured as pull-up input GPIO_Init(GPIOA,
&GPIO_InitStructure);// according to GPIO_InitStructure Initialize the peripheral with the parameters specified in GPIOD register } // Follow the trail ,, Tracking module
Black line output high level 1 White line low level 0 // The left end of the car is O4---- On the far right is O1 // Follow the road : White road black guide line , That is to find the black line . // Black line Sensor output 1, White line output 0
void Find(void) { // It's all white lines , forward if((Find_O4 == 0)&&(Find_O3 == 0)&&(Find_O2 == 0)&&(
Find_O1== 0))// White line , forward { Car_Go(); } O2 On the black line There's a black line on the right Car left // Turn right and adjust to the forward direction . That is, the left wheel accelerates and the right wheel decelerates if
((Find_O4 == 0)&&(Find_O3 == 0)&&(Find_O2 == 1)&&(Find_O1 == 0))// O2 Black line found {
Car_Turn_Right(); } O2 On the black line O1 Black line There's a black line on the right The car deviates to the left // Turn right and adjust to the forward direction . That is, the left wheel accelerates and the right wheel decelerates if((Find_O4
== 0)&&(Find_O3 == 0)&&(Find_O2 == 1)&&(Find_O1 == 1))// O2 O1 Black line found {
Car_Turn_Right(); } O1 On the black line There's a black line on the right The car is leaning to the left // Turn right and adjust to the forward direction . That is, the left wheel accelerates and the right wheel decelerates if((Find_O4 == 0
)&&(Find_O3 == 0)&&(Find_O2 == 0)&&(Find_O1 == 1))// O1 Black line found { Car_Turn_Right();
} O3 On the black line There's a black line on the left Car to the right // Turn left and adjust to the forward direction . That is, the right wheel accelerates and the left wheel decelerates if((Find_O4 == 0)&&(Find_O3 == 1)&&
(Find_O2 == 0)&&(Find_O1 == 0))// O3 Black line found { Car_Turn_Left(); } O3,O4 On the black line There's a black line on the left
The car deviates to the right // Turn left and adjust to the forward direction . That is, the right wheel accelerates and the left wheel decelerates if((Find_O4 == 1)&&(Find_O3 == 1)&&(Find_O2 == 0)
&&(Find_O1 == 0))// O3 O4 Black line found { Car_Turn_Left(); } O4 On the black line There's a black line on the left The car deviates to the right
// Turn left and adjust to the forward direction . That is, the right wheel accelerates and the left wheel decelerates if((Find_O4 == 1)&&(Find_O3 == 0)&&(Find_O2 == 0)&&(
Find_O1== 0))// O4 Black line found { Car_Turn_Left(); } // parking if((Find_O4 == 1)&&(Find_O3 ==
1)&&(Find_O2 == 1)&&(Find_O1 == 1))// So the sensors are all in the black line { Car_Stop(); } }
Technology
Daily Recommendation