5942 views|8 replies

654

Posts

26

Resources
The OP
 

14 "Wanli" Raspberry Pi car - motor drive learning [Copy link]

 

The Raspberry Pi motor driver expansion board uses the PCA9685 chip, which is an I2C to 16-channel PWM chip, and then uses 8 of the PWMs to control four AT8236 chips, which is a motor driver chip. That is, the Raspberry Pi indirectly controls the motor through the I2C interface.

Hardware Hookup

Connect the battery and motor wires to the motor expansion board, as shown below.

Python Example

The store provided the process of controlling the motor, including Python and C++. Since it involves drivers, it is no longer something that can be done with a single file. The file structure of the Python routine is as follows.

When Python is running, other library files can be imported. Just run "DCTest.py" and you can see the motor accelerating and decelerating.

C++ Examples

The file structure of the C++ routine is as follows.

Similarly, only "DC_MotoTest.cpp" is a routine, and the others are driver files. The routine code is as follows:

#include "Emakefun_MotorShield.h"
#include <iostream>
int main () {
Emakefun_MotorShield Pwm = Emakefun_MotorShield();
Pwm.begin(50);
Emakefun_DCMotor *DCmotor1 = Pwm.getMotor(1);
Emakefun_DCMotor *DCmotor2 = Pwm.getMotor(2);
Emakefun_DCMotor *DCmotor3 = Pwm.getMotor(3);
Emakefun_DCMotor *DCmotor4 = Pwm.getMotor(4);
DCmotor1->setSpeed(255);
DCmotor2->setSpeed(255);
DCmotor3->setSpeed(255);
DCmotor4->setSpeed(255);
while(1) {
DCmotor1->run(FORWARD);
DCmotor2->run(FORWARD);
DCmotor3->run(FORWARD);
DCmotor4->run(FORWARD);
delay(1000);
DCmotor1->run(BACKWARD);
DCmotor2->run(BACKWARD);
DCmotor3->run(BACKWARD);
DCmotor4->run(BACKWARD);
delay(1000);
}
}

The routine is easy to understand

DCmotor1->setSpeed(255); //Set the speed range to 0~255

DCmotor4->run(BACKWARD); //Set forward and reverse rotation, including RELEASE, BRAKE

But it is more troublesome to run it, but the store provides a Makefile file, the running method is as follows:

  1. Press F4 in the folder to bring up the terminal
  2. Enter "make" in the terminal, and the "out" file will be automatically compiled and generated.
  3. Enter "./out" in the terminal to run

Then you can see the motor rotating forward and reverse continuously.

question

Now that we can control the motor, how do we get the motor speed?

Source code

GitHub:

链接已隐藏,如需查看请登录或者注册

Gitee:

链接已隐藏,如需查看请登录或者注册

Additional content (2022-3-20 10:36): The motor drive module is controlled using the I2C communication protocol, and I2C needs to be enabled in the settings.
This post is from Innovation Lab

Latest reply

Summary of "Wanli" Raspberry Pi car: lb8820265's "Wanli" Raspberry Pi car open source sharing - DIY/Open Source Hardware Zone - Electronic Engineering World - Forum (eeworld.com.cn) Table of contents: "Wanli" Raspberry Pi car launched 1. “Wanli” Raspberry Pi car - Establishing a project warehouse 2. "Wanli" Raspberry Pi car - Python learning (using Thonny) 3. "Wanli" Raspberry Pi car - Python learning (timing task) 4. "Wanli" Raspberry Pi car - C++ learning (compile and run, use geany) 5. "Ten Thousand Miles" Raspberry Pi Car - WiringPi Learning (Delay and Thread Simulation Timer) 6. "Wanli" Raspberry Pi car - wiringPi learning (PWM and external interrupt simulation timer) 7. "Ten Thousand Miles" Raspberry Pi Car——RPi.GPIO Learning (PWM and External Interrupt Simulation Timer) 8. "Wanli" Raspberry Pi car - socket learning (local communication) 9. "Ten Thousand Miles" Raspberry Pi Car - Socket Learning (TCP Two-Machine Communication) 10. "Ten Thousand Miles" Raspberry Pi Car - Socket Learning (UDP Two-Machine Communication) 11. "Wanli" Raspberry Pi car - socket learning (sent from Android) 12 "Wanli" Raspberry Pi car - socket learning (Android sending and receiving) 13. "Wanli" Raspberry Pi Car - Accessories Preparation 14 "Wanli" Raspberry Pi car - motor drive learning 15 "Wanli" Raspberry Pi car - photoelectric encoder learning (forward and reverse judgment) 16. "Wanli" Raspberry Pi car - photoelectric encoder learning (obtaining speed) 17 "Ten Thousand Miles" Raspberry Pi Car——VSCode Learning (Compiling and Debugging) 18. "Ten Thousand Miles" Raspberry Pi Car——Makefile Learning 19 "Ten Thousand Miles" Raspberry Pi Car——VSCode Learning (Multiple C File Link Debugging) 20 "Million Miles" Raspberry Pi Car - Motor Control Learning (Control Speed) 21. "Wanli" Raspberry Pi car - motor control learning (4-wheel speed control) 22. "Wanli" Raspberry Pi car - mobile phone remote control motor rotation 23 "Wanli" Raspberry Pi car - connected to Raspberry Pi without screen 24 "Millions" Raspberry Pi Car - Bullseye Benchmark Test of Raspberry Pi 64-bit System 25 "Million Miles" Raspberry Pi Car - Nam Wheel Control 26 "Wanli" Raspberry Pi car - program startup 27 "Ten Thousand Miles" Raspberry Pi Car - Fix and Get the Raspberry Pi IP Address 28 "Wanli" Raspberry Pi car - car assembly 29 "Wanli" Raspberry Pi car - straight-driving deviation problem and new control mode 30. "Wanli" Raspberry Pi car - Phase 1 completed demonstration (introduction from scratch)   Details Published on 2022-3-21 13:40
Personal signatureQQ:252669569
 
 

1w

Posts

204

Resources
From 2
 

Summary of "Wanli" Raspberry Pi car:

lb8820265's "Wanli" Raspberry Pi car open source sharing - DIY/Open Source Hardware Zone - Electronic Engineering World - Forum (eeworld.com.cn)

Table of contents:

"Wanli" Raspberry Pi car launched

1. “Wanli” Raspberry Pi car - Establishing a project warehouse

2. "Wanli" Raspberry Pi car - Python learning (using Thonny)

3. "Wanli" Raspberry Pi car - Python learning (timing task)

4. "Wanli" Raspberry Pi car - C++ learning (compile and run, use geany)

5. "Ten Thousand Miles" Raspberry Pi Car - WiringPi Learning (Delay and Thread Simulation Timer)

6. "Wanli" Raspberry Pi car - wiringPi learning (PWM and external interrupt simulation timer)

7. "Ten Thousand Miles" Raspberry Pi Car——RPi.GPIO Learning (PWM and External Interrupt Simulation Timer)

8. "Wanli" Raspberry Pi car - socket learning (local communication)

9. "Ten Thousand Miles" Raspberry Pi Car - Socket Learning (TCP Two-Machine Communication)

10. "Ten Thousand Miles" Raspberry Pi Car - Socket Learning (UDP Two-Machine Communication)

11. "Wanli" Raspberry Pi car - socket learning (sent from Android)

12 "Wanli" Raspberry Pi car - socket learning (Android sending and receiving)

13. "Wanli" Raspberry Pi Car - Accessories Preparation

14 "Wanli" Raspberry Pi car - motor drive learning

15 "Wanli" Raspberry Pi car - photoelectric encoder learning (forward and reverse judgment)

16. "Wanli" Raspberry Pi car - photoelectric encoder learning (obtaining speed)

17 "Ten Thousand Miles" Raspberry Pi Car——VSCode Learning (Compiling and Debugging)

18. "Ten Thousand Miles" Raspberry Pi Car——Makefile Learning

19 "Ten Thousand Miles" Raspberry Pi Car——VSCode Learning (Multiple C File Link Debugging)

20 "Million Miles" Raspberry Pi Car - Motor Control Learning (Control Speed)

21. "Wanli" Raspberry Pi car - motor control learning (4-wheel speed control)
22. "Wanli" Raspberry Pi car - mobile phone remote control motor rotation

23 "Wanli" Raspberry Pi car - connected to Raspberry Pi without screen

24 "Millions" Raspberry Pi Car - Bullseye Benchmark Test of Raspberry Pi 64-bit System

25 "Million Miles" Raspberry Pi Car - Nam Wheel Control

26 "Wanli" Raspberry Pi car - program startup

27 "Ten Thousand Miles" Raspberry Pi Car - Fix and Get the Raspberry Pi IP Address

28 "Wanli" Raspberry Pi car - car assembly

29 "Wanli" Raspberry Pi car - straight-driving deviation problem and new control mode

30. "Wanli" Raspberry Pi car - Phase 1 completed demonstration (introduction from scratch)

This post is from Innovation Lab
Add and join groups EEWorld service account EEWorld subscription account Automotive development circle
Personal signature

玩板看这里:

https://bbs.eeworld.com.cn/elecplay.html

EEWorld测评频道众多好板等你来玩,还可以来频道许愿树许愿说说你想要玩的板子,我们都在努力为大家实现!

 
 
 

1412

Posts

3

Resources
3
 

Are all Raspberry Pis so powerful?

This post is from Innovation Lab

Comments

In fact, the Raspberry Pi has more disadvantages now. In terms of timing, it is not even as good as a single-chip microcomputer, which means that the Raspberry Pi can basically not do many things that a single-chip microcomputer can do.  Details Published on 2021-12-7 21:53
 
 
 

5221

Posts

239

Resources
4
 

Summary (16 articles updated): lb8820265's "Wanli" Raspberry Pi car open source sharing

This post is from Innovation Lab
Add and join groups EEWorld service account EEWorld subscription account Automotive development circle
 
 
 

654

Posts

26

Resources
5
 
annysky2012 posted on 2021-12-7 08:32 Are all Raspberry Pis so powerful? ?

In fact, the Raspberry Pi has more disadvantages now. In terms of timing, it is not even as good as a single-chip microcomputer, which means that the Raspberry Pi can basically not do many things that a single-chip microcomputer can do.

This post is from Innovation Lab

Comments

I see that many motion control systems online use Raspberry Pi as the baseboard?  Details Published on 2021-12-8 11:27
Personal signatureQQ:252669569
 
 
 

7462

Posts

2

Resources
6
 
lb8820265 Published on 2021-12-7 21:53 In fact, now that we are in contact with it, the Raspberry Pi has more disadvantages. In terms of timing, it is not even as good as the microcontroller, which leads to many things that the microcontroller can do, the Raspberry Pi...

I see that many motion control systems online use Raspberry Pi as the baseboard?

This post is from Innovation Lab

Comments

Yes, of course it can be done, but the effect is definitely not as good as using a single-chip microcomputer for motion control. Otherwise, there wouldn’t be so many solutions that use a single-chip microcomputer for motion control and then use the main CPU to run the visual algorithm, including DJI’s master car and all drones.  Details Published on 2021-12-8 22:00
Personal signature

默认摸鱼,再摸鱼。2022、9、28

 
 
 

654

Posts

26

Resources
7
 
freebsder posted on 2021-12-8 11:27 I see that many motion control systems on the Internet use Raspberry Pi as the baseboard?

Yes, of course it can be done, but the effect is definitely not as good as using a single-chip microcomputer for motion control. Otherwise, there wouldn’t be so many solutions that use a single-chip microcomputer for motion control and then use the main CPU to run the visual algorithm, including DJI’s master car and all drones.

This post is from Innovation Lab

Comments

Oh, I thought the control part was also a Raspberry Pi. So that's what it is.  Details Published on 2021-12-9 17:46
Personal signatureQQ:252669569
 
 
 

7462

Posts

2

Resources
8
 
lb8820265 Published on 2021-12-8 22:00 Yes, of course it can be done, but the effect is definitely not as good as using a single-chip microcomputer for motion control, otherwise there would not be so many solutions that use single-chip microcomputers for motion control...

Oh, I thought the control part was also a Raspberry Pi. So that's what it is.

This post is from Innovation Lab
Personal signature

默认摸鱼,再摸鱼。2022、9、28

 
 
 

33

Posts

1

Resources
9
 
Thanks for sharing! Thanks for sharing! Thanks for sharing!
This post is from Innovation Lab
 
 
 

Guess Your Favourite
Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list