Teach you to do the Bluetooth car (three)

Source: Internet
Author: User
Tags what interface

6th Section Motor

To say Bluetooth car which module is most important, most people will think it is a motor.
Previously said, in order to prevent the Development Board by the current breakdown, the control motor to add a piece of expansion board.
Therefore, the control motor, only the expansion board programming, and do not need to program the motor.
In addition, the expansion board manufacturers will provide code to control the motor through the expansion board.

In summary, for the developer, the motor, as long as the confirmation exists.

6.1 Expansion Board

The Arduino Development Board provides only a few basic, universal interfaces, and for some common special features, the Arduino specifically launches an expansion board for it.

6.1.1 Official Expansion Board

Arduino official currently has a total of 5 expansion boards.
respectively is
Arduino Motor Shield

Arduino Proto Shield

Arduino Ethernet Shield

Arduino GSM Shield

Arduino WiFi Shield 101

One of the Arduino Motor Shield is an expansion board designed specifically for motors.

6.1.2 third-party expansion boards

Arduino is an open platform, so there are a number of third-party designers designing expansion boards for Arduino.
Today we are going to use the following dual l293d chip motor expansion board.

As a software programmer, we just need to know that l293d is an "H-bridge motor driver" enough.
If you have more curiosity, you can see l293d's datasheet file

This motor expansion board was chosen instead of the officially launched expansion board.
One reason is that the expansion board has two l293d, which supports the simultaneous control of four wheels. Four-wheel drive, good cow x sensation:)
Another reason is that the development board is cheap.

6.2 Motors and Wheels

The motor adopts the more common

As long as the wheels can match the motor.

Because the two devices are not programmed, there is not much to ask for.

6.3 Code Analysis

Manufacturers to provide some extension board-related code, I put the relevant three files packaged in the bean Network for easy download.

6.3.1 Extension Board library file

After downloading the extract, open the Motortest directory and you will see three files.

Afmotor.cpp,afmotor.h is the library file for the extension board.
There are two ways for Arduino to use library files.

The first method is to add them to the Arduino library file directory.
Make the library file into a ZIP package, package files directly or pack them in a directory.
Choose menu item, Load library, add one. Zip Library, the file is added to the "c:\Users\UserName\Documents\Arduino\libraries\" directory.
So you can also directly copy these two files to the above directory.

The second method is to put the two files together with their Ino files.
The library file is called according to relative paths when used.

All Arduino modules, the manufacturer will provide the library files, this library file equivalent to the SDK.
Most of the time, we don't need to know the detailed implementation of it, as long as we know what interface it provides and how to use it.

6.3.2 Expansion Board test program

The Motortest.ino is a demonstration program for this motor expansion board.
We need to know more about this file.

6.3.2.1 Referencing header files

Line 5th
#include <AFMotor.h>
If you do not put AFMotor.h in the Arduino library file directory, this is changed to a "" reference header file.

6.3.2.2 Generating Motor objects

As you can see from this diagram, the four motors that the Development Board can control are labeled M1, M2, M3, M4, respectively.
Line 7th
AF_DCMotor motor(4);
Generates an object that controls the M4 number motor.

6.3.2.3 Setting the motor speed

Line 14th
motor.setSpeed(200);
Judging by the function name is the speed at which the motor is set, which is enough for the code to understand.
Of course, if you are very curious, I still need to be satisfied.

Look at the implementation of the Setspeed function:

void AF_DCMotor::setSpeed(uint8_t speed) {  switch (motornum) {    case1:      break;    case2:      break;    case3:      break;    case4:      break;  }}

The parameter we passed in is 200, that is speed=200 .
When initializing the Af_dcmotor object, specify the M4 number motor, here motornum等于4 .
The setspeed is ultimately executed case 4: setPWM4(200); .

And look at the implementation of the SETPWM4 function:

inline  void  setPWM4 (uint8_t s) { #if defined (__avr_atmega8__) | | \      defined (__avr_atmega48__) | |     Defined (__avr_atmega88__) | |     Defined (__avr_atmega168__) | | Defined (__avr_atmega328p__) //use PWM from timer0a on PB3 (Arduino pin #6)  ocr0b = s;  #elif defined (__avr_atmega1280__) | | defined (__AVR_ATMEGA2560__)  //on Arduino Mega, pin 6 are now PH3 (oc4a)  ocr3a = s;   #elif defined (__pic32mx__)   //Set the OC2 (pin 5) PMW duty cycle from 0 to 255  OC2RS = s;  #else   #error "This chip was not supported!"   #endif }  

The macro definition I have on hand for this mega board is __avr_atmega2560__ , which executes the code OCR3A = S; .
You can use this method if you are not sure which macro definition corresponds to your development board.
Every # # # is a bit of code, not the same.

inline  void  setPWM4 (uint8_t s) { #if defined (__avr_atmega8__) | | \  defined (__avr_ atmega48__) | | Defined (__avr_atmega88__) | | Defined (__avr_atmega168__) | | Defined (__avr_atmega328p__) //use PWM from timer0a on PB3 (Arduino pin #6)  ocr0b = s; Abc #elif defined (__avr_atmega1280__) | | defined (__AVR_ATMEGA2560__)  //on Arduino Mega, pin 6 are now PH3 (oc4a)  ocr3a = s; Def #elif defined (__pic32mx__)  //Set the OC2 (pin 5 ) PMW duty cycle from 0 to 255  OC2RS = s; Ghi #else   #error "This chip was not supported!"   #endif } 

Compile, and the compiler will tell you that a line of code is wrong. I def have an error in the tips here.
I'm just sure my development board corresponds to a __AVR_ATmega1280__ or defined(__AVR_ATmega2560__ .

OCR3A = sWhat does this line of code do? Simply put, just tell Arduino that the output voltage is divided into 255 parts per time, s part output 1, the other output 0.
OCR3A = 200is 200 parts output 1, 55 parts output 0. Overall, the Arduino outputs a 5(v)*200/255~=4(v) voltage.

Still not satisfied with this explanation?
Learn the Mega Pinmapping and PWM two documents yourself.

/*******************************************************************/
* Copyright Notice
* This tutorial is only published in CSDN and the green Bean Network, other websites appear this tutorial is infringing.
/*******************************************************************/

6.3.2.4 Motor Initial state set to stop

Line 16th
motor.run(RELEASE);
Let's look at the definition of release and define four commands in AFMotor.h.

// Constants that the user passes in to the motor calls#define FORWARD  1#define BACKWARD 2#define BRAKE    3#define RELEASE  4

Then the implementation of the run function is analyzed, and the second half of the function is first seen.

void AF_DCMotor::run(uint8_t cmd) {...  switch (cmd) {    case FORWARD:      latch_state |= _BV(a);      latch_state &= ~_BV(b);      MC.latch_tx();      break;    case BACKWARD:      latch_state &= ~_BV(a);      latch_state |= _BV(b);      MC.latch_tx();      break;    case RELEASE:      latch_state &= ~_BV(a);     // A and B both low      latch_state &= ~_BV(b);      MC.latch_tx();      break;  }}

Let's see what _BV it is.
#define _BV(bit) (1 << (bit)), the function is to shift the bit 1 to the left.

We continue to analyse the release order just issued,
latch_state &= ~_BV(a);, the latch_state of the first place is zeroed.
latch_state &= ~_BV(b);To clear the Latch_state B-position.
MC.latch_tx();Call the Afmotorcontroller class Latch_tx function.

Go back and analyze the first half of the run function

void AF_DCMotor::run(uint8_t cmd) {  uint8_t a, b;  switch (motornum) {    case1:      break;    case2:      break;    case3:      break;    case4:      break;    default:      return;  }  ...}

Continue to view the definition of motor1_a

#define MOTOR1_A 2#define MOTOR1_B 3#define MOTOR2_A 1#define MOTOR2_B 4#define MOTOR4_A 0#define MOTOR4_B 6#define MOTOR3_A 5#define MOTOR3_B 7

In combination with this definition, we can draw the conclusion that:
The latch_state variable is 2, and 3 bits correspond to MOTOR1, which is the M1 seen on the expansion board.
1, 4-bit corresponding M2,
5, 7-bit corresponding M3,
0, 6-bit corresponds to M4.

Continue analysis latch_tx function

voidAfmotorcontroller::latch_tx (void) {uint8_t i;//latch_port &= ~_BV (LATCH);Digitalwrite (Motorlatch, low);//ser_port &= ~_BV (SER);Digitalwrite (Motordata, low); for(i =0; I <8; i++) {//clk_port &= ~_BV (CLK);Digitalwrite (MOTORCLK, low);if(Latch_state & _BV (7-i)) {//ser_port |= _BV (SER);Digitalwrite (Motordata, high); }Else{//ser_port &= ~_BV (SER);Digitalwrite (Motordata, low); }//clk_port |= _BV (CLK);Digitalwrite (MOTORCLK, high); }//latch_port |= _BV (LATCH);Digitalwrite (Motorlatch, high);}

This code is based on the status of each bit of latch_state on or off the motordata pin.
If you do not learn the details of this hardware, completely do not understand what this is doing.
At present, it is sufficient to know that the motor can be effectively controlled by these operations.

6.3.2.5 Motor Forward
  ...  motor.run(FORWARD);  for0255; i++) {    motor.setSpeed(i);    delay(10);  }  for2550; i--) {    motor.setSpeed(i);    delay(10);  }  ...

After the analysis, this piece of code is very simple.
motor.run(FORWARD)Let the wheels go forward.
The first for loop I gradually becomes larger, motor.setSpeed(i) making the wheel faster and faster.
The first for loop I gradually decreases, motor.setSpeed(i) making the wheel speed more and more slowly until the last stop.

6.3.2.6 Motor Back
  motor.run(BACKWARD);  for0255; i++) {    motor.setSpeed(i);    delay(10);  }  for2550; i--) {    motor.setSpeed(i);    delay(10);  }

The rest of the code is relatively simple and no longer analyzed.

6.4 Connection Module

The Bluetooth module is also a 5.2-section connection method.
Notice how the expansion board and mega are connected. The expansion board does not have a pin foot on this head and the mega does not have a pin pin on one side, the expansion board 0 pin Pin and the Mege 0 pin pin coincide, as shown in the Yellow line in the figure.

6.5 Testing

Upload the program to the Development Board, you can observe the motor forward, speed from slow to fast, and from fast to slow.
Then the motor turns backwards, from slow to fast, and from fast to slow.

Teach you to do the Bluetooth car (three)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.