Build a ROS car-lower computer (based on arduino mega2560, L298N)

The communication between ROS and arduino uses ros_arduino_bridge. The code is also modified according to the code of the school brother . The brother said that it can be used directly by burning, but I still encountered many problems. Debug is uncomfortable. The following operations are for reference only and need to be based on Adjust according to the actual situation

Use of L298N

Insert picture description here

Connection method

  1. No speed adjustment: Connect directly to IN1 and IN2, and the ENA and ENB keycaps do not move (the function of the keycap is to make ENA and ENB connect to 5V by default, so as to drive the motor to rotate. Only the corresponding motor when ENA and ENB are at high level To rotate)
  2. Speed ​​adjustment: After IN1.2.3.4 is connected to arduino, connect the pins of ENA and ENB on the parallel side of IN to the PWM output pins of arduino, and input PWM pulses to ENA and ENB enable terminals
    • There is also an article saying that the pwm output of arduino can be directly connected to IN1 and IN2 to adjust the speed, but after many attempts, it is found that it does not work.

Connection with arduino

It should be noted here that L298N and arduino must share the same ground, and directly connect GND to arduino's GND. When debugging, you can connect the 5V port of L298N to arduino for power supply (because 5V can be used for output when the power supply port keycap is not unplugged)

附上接线部分代码

//电机定义,ENCODER_1代表编码器正极,A标号为左侧电机
#define  PWMA  10
#define  AIN1  4
#define  AIN2  5
#define  AENCODER_1     3    //INT1--中断号 只能读取B相的
#define  AENCODER_2     2    //INT0
#define  PWMB  11
#define  BIN1  6
#define  BIN2  7
#define  BENCODER_1     18   //INT5
#define  BENCODER_2     19   //INT4
//需要注意arduino mega的中断引脚以及引脚号,后边会用到,如果进行修改的话需要按照自己的接线来修改

arduino

The reasons for using mega are as follows

  • There are many pins, which is also the board used in the original code of ros_arduino_bridge
  • There are many interrupt pins, if you want to make a four-wheeled or three-wheeled car in the future, you can use it
  • There are also more power pins, so you don’t need to solder the header pins externally.

Note: The original version is more expensive, here I use a domestic board

The code has been uploaded to github and updated synchronously. The link is attached at the end of the article. The following explains the code (there are already a lot of comments in the code, here are only some that I think are important) and the problems encountered during the project. Friends with the same problem can discuss together

  • A lot of macro definitions are used in the code. You need to pay attention to this part of the code when you read the code. The context is combined
  • The communication part is used for communication between upper and lower computers, and can be modified according to your needs
  • Interruption: There is a hidden trick in interruption, because the direction of the positive and negative poles of the encoder is the same, but the direction is opposite after being installed on the wheel, so if the encoder 1 enters the interrupt corresponding to the positive pin, then encode In this way, when the robot moves in a certain direction, the two encoders can enter the interrupt at the same time. If the lines corresponding to the positive poles of the two encoders are interrupted, the first few values ​​may have positive and negative values ​​jump
  • pid debugging: You can refer to this article here , but the pid code of ros_arduino_bridge feels a bit strange, because I once encountered a problem. When the power is not turned on, pid.out will slowly increase to 255 and rotate at full speed. It is presumed that it is a pid problem.

Code: https://github.com/LinErTe/roscar

Guess you like

Origin blog.csdn.net/LLeeeTT/article/details/114080834