![Board.png]

- 0_Car expansion board: the main board, drive motor, servo, onboard domestic attitude sensor and magnetometer, CAN circuit, buzzer, etc.
- 1_External Bluetooth module: use domestic Bluetooth module for dual-car communication
- 2_Liangshanpai-K210 fixed board: structural parts, carrying Liangshanpai and K210 development board
- 3_Car bottom plate: structural parts
- 4_RGY light board: to meet the requirements of the topic, display the working status of the car
- 5_Camera fill light board: to fill light for the camera when the ambient brightness is too low, and also used to fix the camera position
# Code repository: [https://gitee.com/lcsc/medical_car](https://gitee.com/lcsc/medical_car)
# **All-in-one localization**
Why do we need to do all-in-one localization? This can be regarded as an opportunity given by the previous epidemic. STM32 has been crazy in the first few years of the epidemic, and the global supply chain has been greatly impacted during the epidemic. Many people say that the price increase of graphics cards is too outrageous. In fact, in the past few years of the epidemic, in terms of the price increase, the price increase of chips has surpassed that of graphics cards.
Therefore, we must begin to focus on independent innovation and technological independence. Now many industries in China are promoting domestic components, which can not only improve the domestic technical level, but also reduce dependence on external technology and enhance overall competitiveness. I hope that everyone can use more domestic components in work and study. Of course, this is not blind support. At present, the gap between many chips and foreign technology is indeed not small. Don't go to extremes and can't be used as diodes. If domestic chips can meet the project requirements, support them. If not, it is understandable to use foreign ones.
# **Two pin allocation**
At the beginning of circuit design, the most important thing is pin allocation. Poor pin allocation may cause the MCU with abundant resources to have few interfaces available, or cause abnormal wiring troubles. Good pin allocation can effectively determine the position and connection method of each component on the circuit board.
Combining the Liangshan School schematic diagram and the hardware definition in 2.6.2 of the GD32F470 [Datasheet](https://www.gd32mcu.com/data/documents/datasheet/GD32F470xx_Datasheet_Rev1.4.pdf), we can get the following allocation table. Because GD32 has not launched a graphical configuration tool like Cubemx, it can only be recorded manually at present, because some of the pins led out by the pin header are occupied by the SPI Flash, LED, SDIO, etc. of the development board, and some will be connected to pull-up and pull-down resistors. So be sure to pay attention to the allocation and don't be careless. The pins connected to the screen FPC seat can be used as long as you don't use the screen.
![1280X1280 (1).PNG]

## [**P1** allocation diagram](https://gitee.com/lcsc/medical_car/blob/master/4_Docs/images/ROGxO4UU92k8q0dDzYZY7ZFrqGIA8vSzGOcAuQYy.jpeg)
## [**P2** allocation diagram](https://gitee.com/lcsc/medical_car/blob/master/4_Docs/images/ROGxO4UU92k8q0dDzYZY7ZFrqGIA8vSzGOcAuQYy.jpeg)
When I chose, I first selected UART, SPI, orthogonal encoder interface, PWM, IIC (hardware first, software is also acceptable), CAN, ADC, LED, KEY, and grayscale sensor interface in this order. The specific choice depends on how you rank the importance of these resources.
UART:
- USART2 (RX: PB11; TX: PB10) -- K210
- USART5 (RX: PG9; TX: PG14) -- Wireless module
- USART1 (RX: PD6; TX: PD5) -- Remote control standby
- UART3 (RX: PA1) -- S-BUS reserved
SPI:
- SPI3
- CS0 (PE4)
- CS1 (PC13)
- SCK (PE2)
- MOSI (PE6)
- MISO (PE5)
IIC:
- I2C0 (SCL: PB6; SDA: PB7)
- Analog I2C (SCL: PG6; SDA: PG7)
PWM:
- TIMER8_CH0 (PA2) -- Motor M1-IN1
- TIMER8_CH1 (PA3) - Motor M1-IN2
- TIMER11_CH0 (PB14) - Motor M2-IN1
- TIMER11_CH1 (PB15) - Motor M2-IN2
- TIMER7_CH0 (PC6) - Servo 1
- TIMER7_CH1 (PC7) - Servo 2
- TIMER12_CH0 (PA6) --Buzzer
- TIMER0_CH0 (PA8) - Spare
Encoder:
- TIMER2_CH0 (PB4) - Motor M1-Encoder A
- TIMER2_CH1 (PB5) - Motor M1-Encoder B
- TIMER1_CH0 (PB8) - Motor M2-Encoder A
- TIMER1_CH1 (PB9) - Motor M2-Encoder B
ADC:
- ADC01_IN4 (PA4)
- ADC012_IN11 (PC1)
- ADC01_IN7 (PA7)
- ADC01_IN14 (PC4)
LED:
- LED-R (PD3)
- LED-G (PG10)
- LED-Y (PA15)
KEY:
- Collision switch KEY0 (PG12)
- KEY1 (PG11) - KEY2
(PG13)
- KEY3 (PB3)
CAN:
- CAN0 (RX: PD0; TX: PD1)
can also be used (grayscale sensor spare)
- PD4
- PB1
- PB0
- PB12
- PB13
- PF10
- PC5
For specific allocation, please refer to the final version of the schematic
# **Three-component selection and schematic design**
In order to cooperate with Liangshan School's **nationalization**, this PCB must also be
selected for nationalization, so only domestic components are used when selecting the schematic.
## **1. Power path selection and design**
![9797d40b-1f10-414b-a035-bafbb43d0cc8.png]

Select the [SGM6132](https://item.szlcsc.com/88273.html) of Shengbang Microelectronics, 3A, 28.5V, 1.4MHz Step-Down Converter.
Refer to the official typical application to design the schematic diagram,
![62719979-6389-4a4f-ba38-78b1972fb0b0.png]

One of the good things about this chip is that it has an enable pin. If you want to use it, you can control the working state of the chip. If you don't want to connect it, it can also start automatically.
Generally, the official will give a reference schematic diagram for this kind of power chip. The typical application is given in the lower left corner of the first page of the SGM6132 datasheet. But it is 3.3V. We can find the typical application circuit on page 10, which has the recommended values of various parameters.
![f7ce3298-87cd-42d0-8ec5-8e7bf04c893a.png]

What I want here is 5V output. Combining the above, you can know the specific component selection parameters, and you don’t have to calculate it yourself. If you calculate the resistance value yourself, one thing you should pay attention to is that the resistance value should not exceed its maximum value. For example, the maximum value of R2 of this chip is 100kΩ. If it is too large, it may not work. The simplest and most reliable way is to use the official recommended diagram to design.
The resistors and capacitors and the one-piece metal inductor here are all produced by domestic manufacturers.
The selection of other XT30 interfaces, resettable fuses, fluctuation switch circuits, TVS overvoltage protection, etc. is relatively simple.
![output (16).png]

There are not many well-known manufacturers of XT30 male connectors, so you can choose any one.
![eeed55c2-16cb-4f5e-aa16-c85d0391c884.png]

Considering that the instantaneous current of the two DC reduction motors will be very large when starting, even if 2A is allocated to each motor, 1A is left for the development board, so the maximum 5A resettable fuse is selected here.
![7b332d53-c140-4a97-8617-8fc63391e339.png]

The toggle switch selected here can pass a maximum current of 6A, which is enough.
![ba88f1a0-045f-4df1-914a-b9bcbc1eb32d.png]

Its working principle is that when working under normal voltage, the resistance of the TVS protection circuit is very large and will not affect the circuit; but when transient voltage appears, the resistance of the TVS protection circuit will quickly decrease, limiting the voltage within the set safety range, thereby protecting the electronic equipment from damage. However, the voltage of lithium batteries is very stable and pure. This protection is mainly used to prevent voltage from exceeding the standard when using a switching power supply during debugging.
## **2. Selection and design of motor drive circuit**
![output (17).png]

An AT8870 can drive a DC motor in forward and reverse directions. We need to drive two DC motors, so we need to use two driver chips here. Its manual is all in Chinese. The key parameters to look at are the main power supply requirements, logic power supply requirements, logic input requirements, continuous output current, peak output current, maximum PWM frequency, etc.
After understanding the above parameters, we can choose the motor. We must ensure that the working current and voltage of the motor are below the working range of the motor driver chip. I chose MG513P20_12V from Lunqu Technology. The motor has a reduction ratio of 20, an operating voltage of 12V, a rated current of 0.36A, and a stall current of 3.2A. The peak output current of the AT8870 we use is 3.6A, so it is still possible to burn out in the case of long-term stalling, so we need to set the protection of the driver chip. The ISEN pin of this chip can set the current control current of the chip (the value of this resistor is related to the reference voltage and the current to be set). For detailed introduction, please see page 8 of the AT8870 datasheet, which has detailed introduction and formula. In the schematic diagram above, I chose 0.15 ohms. It means that when the reference voltage is 3.3V, the target current is 2.2A.
In addition, let's take a look at what fast decay and slow decay mean. The speed here refers to the current, not the speed of the motor.
Since the motor is an inductive load, when the voltage at both ends of the motor is disconnected, the current will generate reverse electromotive force. This may cause damage to the driver chip. Therefore, in order to stop the motor, in addition to disconnecting the power supply, it is also necessary to establish a freewheeling circuit to release the energy in the motor. Slow decay is equivalent to the disappearance of the voltage applied to both ends of the motor (inductive component), and the positive and negative ends of the motor are short-circuited. Fast decay is equivalent to the disappearance of the voltage applied to the two ends of the motor (inductive component), and the two ends of the motor are quickly connected to the current opposite to the driving current.
>Recently, I found that the AT8870 on the LiChuang Mall has stopped being purchased. Then you can choose a better one, **AT8236**, which has a maximum peak drive output of 6A and a continuous output drive of 4A. It is also PINtoPIN, which can be directly replaced. Just replace it directly during actual welding.
## **3. Selection and design of attitude sensor**
The attitude sensor is a sensor that measures the attitude of an object. It can calculate the attitude information of the object by measuring the acceleration, angular velocity and other parameters of the object. The most famous one is mpu6050, which is the world's first integrated 6-axis motion processing component launched by InvenSense. InvenSense has been bought by Japan's TDK. The latest IMUs now all start with ICM, such as ICM-42688. You can take a look at the introduction of this webpage. Models starting with mpu and icm-2x are no longer recommended (some have been discontinued). Science and technology are developing very fast, and the manufacturing process and accuracy of these sensors are also improving rapidly. So if you are learning or making products, it is best to use new products. Electronic products are bought new rather than old, and the update is too fast. If the sensor is not easy to use, it will be difficult for the software algorithm.
Generally, there are several ways to obtain attitude information. The first and simplest way is to buy a module directly, and the module will do attitude solution inside. You only need to process the attitude information directly, which is usually transmitted through the serial port. The second way is that the IMU chip has its own solution algorithm. For example, the MPU6050 comes with a DMP library. After it is configured on the MCU side, the chip's built-in DMP acceleration can be used for solution. The third way is to only obtain the raw data of the IMU, and then solve it yourself inside the MCU. This is the most difficult. If you don’t spend enough time, you may end up with a mess, and the data obtained cannot be used, but this is also the way you learn the most.
Although there is still a big gap between the six-axis attitude sensors produced by domestic manufacturers and those produced abroad, there is basically no supply risk and the price is quite affordable. You can open the search on the LiChuang Mall and you can see that the two models with the highest domestic sales are QMI8658A and QMI8658C. Try to choose model A, which is an upgraded version of model C and has lower noise. Its package is also PIN to PIN with some models of foreign manufacturers, and it can be replaced and tested with imus of other manufacturers later.
Refer to page 8 of the datasheet of [QMI8658A](https://item.szlcsc.com/3544058.html) (now the PDF preview below the LiChuang Mall can view the entire manual), he gives the application diagrams of three-wire SPI, four-wire SPI, and IIC. Looking at Table 15 in the manual, you can see that the SPI clock of this chip supports up to 15Mhz and the IIC clock supports up to 400Khz. In order to obtain more data per unit time, the SPI protocol is selected.
![757cdddc-0699-46ae-9122-a2f2e4285d20.png]

## **4. Selection and design of magnetometer sensor**
The attitude sensor is divided into 6-axis and 9-axis. The 9-axis one is equipped with a magnetometer, but there seems to be no 9-axis attitude sensor made in China.
In attitude solution, the magnetometer can provide important geomagnetic information to help determine the direction and position of the device. If there is no magnetometer and only accelerometer and gyroscope are used to solve the attitude, then in some cases, such as when the device is stationary or only moves on a horizontal plane, the direction of the device cannot be determined. By using a magnetometer, the earth's magnetic field can be measured, and the direction and position of the device can be determined, thereby achieving more accurate attitude solution. In addition, the magnetometer can also help correct the drift error of the gyroscope and improve the accuracy of attitude solution.
The performance of domestic magnetometers is quite good now. I have seen that some flight controllers have begun to use QMC5883L. Now Shanghai Si Rui has launched the newer QMC5883P, which has a much larger range than before.
Referring to its datasheet, we can see that it supports up to 400Khz IIC rate, but does not support SPI. Combining the external connection suggestions on page 7, we can design the following schematic diagram.
![8b061237-601d-404f-b784-31cf543333d9.png]

## **5. Buzzer selection and design**
The buzzer can convert electrical signals into sound signals, and can provide sound feedback or alarm signals to users.
There are two types of buzzers in terms of structural type: electromagnetic and piezoelectric. In terms of driving mode, there are two types: passive (driven by external square wave) and active (driven by internal, external power supply). According to different packaging methods, it can also be divided into pin type and patch type. Then start to open the LiChuang Mall to start selecting, or choose domestic products. Generally speaking, the action voltage of electromagnetic buzzers can be relatively low.
I chose a passive electromagnetic patch buzzer with an operating voltage of 2-4v and a frequency of 4000Hz. The frequency here refers to the loudest sound at this frequency.
![63a2a1d6-c0bd-4524-a3fd-1bd13689acb9.png]

The main function of D2 here is to protect the MOS tube of the driver, because the buzzer is an inductive element like a motor, which means that its current cannot change transiently. There must be a freewheeling diode to provide freewheeling. If there is no freewheeling diode, when the power supply to the buzzer is stopped, there will be a reverse induced electromotive force at both ends of the buzzer, generating a peak voltage of up to tens of volts, which is likely to damage the drive circuit.
R26: Current limiting resistor to prevent the current from being too large to damage the PWM output pin of the chip. R4 is a simple pull-down resistor.
## **6. Button and drug detection switch design**
![2030558f-0220-42f4-a491-6cf55740c90c.png]

This is a very common button design. One thing to note is that when transferring the schematic diagram to the PCB, you should check the package carefully to avoid connecting the pins incorrectly.
The buttons we usually use are all mechanical elastic switches. When it is pressed and bounced, the mechanical contacts will be accompanied by a series of jitters at the moment of closing and opening due to the elastic effect. This jitter will cause the input signal to bounce between high and low potentials, resulting in incorrect input.
The role of the resistor here is to limit the current (I am afraid that beginners will accidentally set it to push-pull output). What needs to be noted here is that a pull-down should be set inside the chip. The role of the capacitor is hardware de-jittering, but software de-jittering is still required in software. Hardware de-jittering can only improve but not eliminate it.
![733bb4ff-c6fa-43c3-bc52-fd7fe8befcfc.png]

The button here is changed to a collision switch. A 3D printed structure is designed. When the medicine is put down, the collision switch is closed to detect whether the medicine is installed successfully.
- No.: [C87120](https://item.szlcsc.com/88298.html), also called a travel switch, the micro-switch button in the mouse is actually this, and the mouse does not have that large patch.
## **7. ADC voltage acquisition circuit design**
![output (18).png]

The first ADC on the left is used to collect whether the 12V voltage from the battery is normal, and the other three are used for users. The choice of resistors here is a simple resistor series voltage division. For example, 12V comes from above, 12V*(R15/(R11+R15))=1.09V, this is the voltage to the MCU IO, the maximum voltage coming from the ADC pin cannot exceed 3.3V, so a D3-bidirectional TVS tube is used, and it protects the MCU pin if it is greater than 3.3V.
## **8. CAN chip selection and design**
The controller area network bus (CAN, Controller Area Network) is a serial communication protocol bus for real-time applications. It is a serial communication protocol bus developed by BOSCH (Bosch) in Germany. It can use twisted pair to transmit signals and is one of the most widely used field buses in the world. It was mainly used for communication between various components in the car before.
Open the LiChuang Mall and start selecting domestic CAN chips. Here I choose SIT (Xinlit)'s SIT65HVD230DR, 3.3V power supply, and a maximum rate of 1Mbps.
![c764fac0-51ce-4e6e-98e7-fa0fee5008ee.png]

VREF pin is Vcc/2 reference output pin, do not connect if not used. Rs is mode selection pin, strong pull down to GND = high speed mode; strong pull up to VCC = low power mode; pull down to GND through 10kΩ to 100kΩ resistor = slope control mode. There is no requirement now, just connect to ground and set to high speed mode.
You can know other connections by referring to the manual. Note that TXD here is a little different from RXD and the general marking method of the serial port. RX is connected to R and TX is connected to D. Note that the CAN bus requires two 120Ω terminal resistors. Terminal resistors must be connected at both ends of the CAN bus to work properly. The role of the resistor is to match the bus impedance and improve the reliability of data communication.
>This 120 ohms is not calculated, but measured. The characteristic impedance measured by the typical twisted pair commonly used in automobiles is about 120 ohms.
## **9.S-BUS circuit design**
SBUS is a serial communication protocol, usually used to connect the remote control and the controlled. It can transmit data of multiple channels through a single channel. It can be understood as a serial port protocol, using a 100k baud rate, eight data bits, two stop bits, and even parity. Because it uses a reverse level output, it must be hardware-inverted, unless your chip serial port peripheral has built-in hardware inversion.
![bfc13714-87e8-4b1f-b710-5d5766f8d478.png]

# **Four PCB Layout**
When making PCBs, although everyone pursues a successful version, a successful version is hard to come by. Don't be discouraged even if you fail. Making mistakes is also a learning process, as long as you don't make the same mistakes repeatedly.
## **1. Pre-layout**
After the schematic is drawn and converted to PCB, the first step is to pre-layout. The quality of the layout largely determines the difficulty of wiring.
In order to keep the size consistent with Liangshanpai, we first copy the frame and positioning hole coordinates of Liangshanpai, and then determine the coordinates of the two 40P headers. Because we want to place the GH1.25 horizontal connector with a lock on the back, the header here needs to be a patch, otherwise the back can't be placed.
![e4ec7548-d0a6-4216-a70d-8ead2e24a054.png]

Then put the components with larger size and area first. The larger connectors here are the XT30 power interface, the high-current toggle switch, the two motor connectors, and the two-way servo interface.
![output (19).png]

This is actually not very good. The span of the 12V power supply is too large, from the bottom to the top of the motor driver chip, but I haven't figured out a good way to place it. Next is the attitude sensor and magnetometer. In order for the attitude sensor to better reflect the attitude of the board, try to put it in the center, and put the magnetometer next to the attitude sensor. Theoretically, in this kind of working condition with motor movement and vibration, the IMU should be a separate board with a shock absorber, but for cost and difficulty considerations, just put it directly on the bottom plate. The left side of the red circle in the figure below is the attitude sensor, and the right side is the magnetometer. The two upper and lower slots have two main functions. One is to reduce the stress effect of plate deformation on the attitude sensor, and the other is to reduce the interference caused by DCDC voltage reduction and motor driver chip operation.
![7f43059f-0042-48c3-9402-8ab7aa607a16.png]

The next step is the layout of various connection sockets and buttons, and we are at the back. The GH1.25 connection socket and side button must be close to the edge of the board. In order to prevent the side button from being accidentally touched, it is designed so that it can only be pressed with a fingernail, and the button head is retracted a little.
![output (20).png]

The layout of other components adopts the principle of proximity. For example, the motor driver chip is placed close to the motor seat, the DC-DC circuit is placed close to the XT30 power interface, and other buzzers, CAN chips, etc. are placed wherever is convenient. The CAN chip should also be placed as close to the CAN signal output socket as possible.
![53218d5b-0481-44fa-adb6-0787dae36e8c.png]

## **2.DC-DC step-down**
The power supply can be said to be the heart of a circuit. An unstable power supply will prevent the system from working properly. When laying out, generally pay attention to the following points:
1. Minimize the loop area as much as possible to reduce electromagnetic interference.
2. Separate the input and output loops to reduce crosstalk.
3. Place the input and output filter capacitors as close to the power device as possible to reduce transient voltage peaks.
4. Lay out the ground wire wide to reduce the crosstalk of the ground loop.
5. Lay out high-frequency components (such as inductors, filter capacitors, etc.) closely to reduce crosstalk between components.
6. Try to avoid cross layout between components to reduce mutual interference.
7. For high-power circuits, it is necessary to consider heat dissipation to ensure that the device works within a safe temperature range.
8. It is best not to use wires, but to use area fill for routing.
Of course, some requirements may not be perfectly met, but try to meet them.
Two typical ones:
* The smaller the SW loop, the better
* ![add65027-300a-4f7b-811b-e49421261abe.png]

* The closer the feedback resistor is to the chip pin, the better
* ![89d99ecc-1ba1-4d24-bb55-aee123f563c5.png]

## **3. Motor drive circuit**
The motor is a high-power electrical appliance, especially when it is stalled, it can reach a current of 2 or 3A. It is absolutely not allowed that the copper foil of the circuit board is directly burned before the chip is burned out. Theoretically, 1OZ copper sheet (thickness is about 0.035mm) can pass a maximum current of about 2A when the width is 1mm. If we use it ourselves, we can be conservative and assume that it can pass 1.8A with 1mm. Therefore, the power line of the motor should be at least 2mm wide. If it is an inner layer, the inner layer copper thickness is generally 0.5OZ and needs to be thicker. If conditions permit, add more margin.
Generally, the wiring of motor drivers is to go to the top and bottom layers as much as possible, the copper sheet thickness is enough, and the heat dissipation is good enough. Directly use solid filling for wiring.
![a749f40f-e828-4a66-ac42-325398d80b20.png]

## **4. Others**
Except for the output of the CAN chip signal, which needs to pay attention to the differential line, there is basically nothing to pay attention to. The wiring should be as short as possible, and the power line of the electrical appliance should meet the current requirements and be thicker than the signal line. The line width, line spacing, vias, characters and other requirements must meet the process requirements of the Jiali Chuang board. If they do not meet the requirements, you may have to pay extra money.
Finally, you must run the DRC test before submitting the board. It is best to run the DRC from time to time during normal wiring to find problems early and treat them early.
After I finished the wiring, it looked like this:
![2023-06-20 11-04-29-19.png]
The car looks like this:
![Car2-1.png]