background image

SunFounder PiCar-S

Here two main functions including the line follower calibration and line following are included in the main program.

Subflow of Line Follower Calibration Function

When we run the line follower configuration, we will start from white color, then black color, which is more like the
upper limit and lower limit of the sensor. Then we take the average value of black and white as reference value: if the
detected value is higher than the reference, it should be white; if the detected is lower than the reference, it should be
black. We will show the five detectors’ status by 5 elements [0,0,0,0,0].

64

Chapter 1. About the PiCar-S

Summary of Contents for PiCar-S

Page 1: ...SunFounder PiCar S www sunfounder com Nov 10 2021 ...

Page 2: ......

Page 3: ...ents List 2 1 2 Introduction 10 1 3 Building the Car 11 1 4 Circuits Building 23 1 5 Get Started with Raspberry Pi 27 1 6 Servo Configuration 37 1 7 Calibration 43 1 8 Arming the Car 46 1 9 Appendix 71 1 10 Thank You 80 2 Copyright Notice 81 i ...

Page 4: ...ii ...

Page 5: ...l show you how to build the PiCar S via description illustrations of physical components in both hardware and software respects You will enjoy learning how all this work You can view the latest PDF user manual or clone the code by click the link https github com sunfounder SunFounder_PiCar S tree V3 0 If you want to learn another projects which we don t have please feel free to send Email and we w...

Page 6: ...r Plate x 1 2 Front Half Chassis x 1 3 Hex Front Wheel Fixing Plate x 8 4 Ultrasonic Support x 1 5 Bearing Shield x 8 6 Steering Linkage x 1 7 Steering Connector x 2 8 Ultrasonic Connector x 1 9 Back Half Chassis x 1 10 Sensor Connector x 1 2 Chapter 1 About the PiCar S ...

Page 7: ...SunFounder PiCar S 1 1 2 SunFounder SF006C Servo x 1 1 Servo x 1 2 1 arm Rocker Arm x 1 3 arm Rocker Arm x 1 4 4 arm Rocker Arm x 1 5 Rocker Arm Fixing Screw x 1 6 Rocker Arm Screw 1 1 Components List 3 ...

Page 8: ...SunFounder PiCar S 4 Chapter 1 About the PiCar S ...

Page 9: ...SunFounder PiCar S 1 1 3 Mechanical Fasteners 1 1 Components List 5 ...

Page 10: ...SunFounder PiCar S 1 1 4 Wires 6 Chapter 1 About the PiCar S ...

Page 11: ...SunFounder PiCar S 1 1 5 PCB 1 1 Components List 7 ...

Page 12: ...SunFounder PiCar S 1 1 6 Other Components 8 Chapter 1 About the PiCar S ...

Page 13: ...to use 18650 batteries without a protective board Otherwise the car may be cut power and stop running because of the overcurrent protection of the protective board 2 For unprotected batteries please purchase those with the anode bulged out as shown below so that it can ensure the well connection with the battery holder 1 1 Components List 9 ...

Page 14: ...ased on Raspberry Pi which comes with three sensor modules including the light follower line follower and ultrasonic obstacle avoidance With these modules this smart car is capable of some simple automatic actions Thus you can learn some basics of programming in Python to control the car with these sensors Let s start with building this smart car 10 Chapter 1 About the PiCar S ...

Page 15: ...CAREFUL observation You should double check your work based on the figures in the manual after finishing each step Don t worry Kindly reminders will be given in some particular steps Just follow the tutorial step by step Okay with no further ado now let s start 1 3 1 Front Half Chassis Assemble the Front Half Chassis with four M3x25 copper standoffs and four M3 nuts as shown below 1 3 Building the...

Page 16: ...the M4 Self locking Nut then use the screwdriver to tighten the M4x25 screw Note The Self locking Nut should be screwed tight enough It would be better to tighten the screw until the wheel and Steering Connector cannot move first then loosen the screw a little so that the Steering Plate can just move Thus the wheel can turn flexibly when the connection would not be too loose Assemble the other fro...

Page 17: ...assembly 1 3 3 Steering Part Connect the Steering Linkage and the 1 arm Rocker Arm with the M1 5x4 Self tapping Screw Note Insert it into the FIRST hole of the arm as indicated by the arrow below which is the farthest from the gears 1 3 Building the Car 13 ...

Page 18: ...SunFounder PiCar S Note Fasten them as tightly as possible and then loosen the screw a little so the Steering Linkage can move flexibly 14 Chapter 1 About the PiCar S ...

Page 19: ...SunFounder PiCar S 1 3 4 Upper Plate Mount the M2 5x8 copper standoffs and M2 5 nuts into the upper plate first Pay attention that the side the protrud ing prop should face up 1 3 Building the Car 15 ...

Page 20: ...hem through the holes on the plate Pay attention to the direction and leave one end longer out of the plate for each to remove the battery easily later Fasten the battery holder with two M3x8 countersunk screws and two M3 nuts pay attention to the direction of battery holder s wire 16 Chapter 1 About the PiCar S ...

Page 21: ...SunFounder PiCar S 1 3 6 Rear Wheels Screws Insert four M3x8 screws with four M3x25 copper standoffs 1 3 Building the Car 17 ...

Page 22: ...SunFounder PiCar S 18 Chapter 1 About the PiCar S ...

Page 23: ... Pi TF Card inserted with eight M2 5x8 copper standoffs then plug the Robot HATS onto it 2 Fix the Robot HATS with four M2 5x6 screws 3 Fix The PCA9685 PWM Driver the Motor Driver with eight M2 5x12 screws and M2 5 nuts into the down plate 1 3 Building the Car 19 ...

Page 24: ...mble the two motors to the Back Half Chassis with four M3x25 screws and M3 nuts Pay attention to place the motors with wires inward providing convenience for connecting the circuit Assemble the rear wheels with 4 M3 nuts 20 Chapter 1 About the PiCar S ...

Page 25: ...SunFounder PiCar S Align the rear wheels with the motor shaft and rotate to insert them gently 1 3 Building the Car 21 ...

Page 26: ...SunFounder PiCar S 22 Chapter 1 About the PiCar S ...

Page 27: ...SunFounder PiCar S 1 4 Circuits Building 1 4 1 Connect the Power 1 4 Circuits Building 23 ...

Page 28: ...SunFounder PiCar S 1 4 2 Connect the Modules 24 Chapter 1 About the PiCar S ...

Page 29: ...SunFounder PiCar S 1 4 3 Connect the Servo 1 4 Circuits Building 25 ...

Page 30: ...SunFounder PiCar S 1 4 4 Connect the Motor The complete connection is shown as follows 26 Chapter 1 About the PiCar S ...

Page 31: ...spberry Pi In this chapter we firstly learn to start up Raspberry Pi The content includes installing the OS Raspberry Pi network and how to open terminal Note You can check the complete tutorial on the official website of the Raspberry Pi https projects raspberrypi org en projects raspberry pi setting up Note If your Raspberry Pi is set up you can skip the part and go into the next chapter 1 5 Get...

Page 32: ... link for the Raspberry Pi Imager that matches your operating system when the download finishes click it to launch the installer Step 2 When you launch the installer your operating system may try to block you from running it For example on Windows I receive the following message If this pops up click on More info and then Run anyway then follow the instructions to install the Raspberry Pi Imager S...

Page 33: ...Imager cache So the next time you open the software it will have the display Released date cached on your computer Step 5 Select the SD card you are using Step 6 Press Ctrl Shift X to open the Advanced options page to enable SSH and configure wifi these 2 items must be set the others depend on your choice You can choose to always use this image customization options 1 5 Get Started with Raspberry ...

Page 34: ...ck SAVE Note wifi country should be set the two letter ISO IEC alpha2 code for the country in which you are using your Raspberry Pi please refer to the following link https en wikipedia org wiki ISO_3166 1_alpha 2 Officially_ assigned_code_elements 30 Chapter 1 About the PiCar S ...

Page 35: ...e WRITE button Step 8 If your SD card currently has any files on it you may wish to back up these files first to prevent you from permanently losing them If there is no file to be backed up click Yes 1 5 Get Started with Raspberry Pi 31 ...

Page 36: ...SunFounder PiCar S Step 9 After waiting for a period of time the following window will appear to represent the completion of writing 32 Chapter 1 About the PiCar S ...

Page 37: ...home network you can check the addresses assigned to Raspberry Pi on the admin interface of router The default hostname of the system Raspberry Pi OS is raspberrypi and you need to find it If you are using ArchLinuxARM system please find alarmpi 2 Network Segment Scanning You can also use network scanning to look up the IP address of Raspberry Pi You can apply the software Advanced IP scanner down...

Page 38: ...Step 2 Type in ssh pi ip_address pi is your username and ip_address is your IP address For example ssh pi 192 168 18 197 Step 3 Input yes Step 4 Input the passcode and the default password is raspberry Step 5 We now get the Raspberry Pi connected and are ready to go to the next step 34 Chapter 1 About the PiCar S ...

Page 39: ...ws Users If you re a Windows user you can use SSH with the application of some software Here we recommend PuTTY You can download from Google Step 1 Download PuTTY Open PuTTY and click Session on the left tree alike structure Enter the IP address of the RPi in the text box under Host Name or IP address and 22 under Port by default it is 22 1 5 Get Started with Raspberry Pi 35 ...

Page 40: ...n to the Raspberry Pi with the IP address there prompts a security reminder Just click Yes Step 3 When the PuTTY window prompts login as type in pi the user name of the RPi and password raspberry the default one if you haven t changed it 36 Chapter 1 About the PiCar S ...

Page 41: ...e battery and slide the power switch to ON in following chapters 1 6 1 Get Source Code You can find the source code in our Github repositories Download the source code by git clone cd home pi git clone recursive https github com sunfounder SunFounder_PiCar S git b V3 0 Note Please pay attention to your typing if you get the prompt of entering your user name and password you may have typed wrong If...

Page 42: ...e servo install is for servo adjustment which is used after the front wheels are assembled The servo will rotate to 90 degrees after this command is run so we will use this command here picar servo install Note If the OSError Errno 121 Remote I O error error message appears open raspi config sudo raspi config Then choose 3 Interfacing Options P5 I2C YES OK to enable I2C service You can use the up ...

Page 43: ...ial website www sunfounder com to buy 1 6 5 Build the Rest of the Car Warning Please keep the command servo install running in the whole process of assembly Mount the steering servo to the Upper Plate with two M2x8 Screws and two M2 nuts pay attention to the direction of the servo wires The Rocker Arm is facing straight ahead and then inserted into the Servo shaft and then fixed with Rocker Arm Fi...

Page 44: ...SunFounder PiCar S Mount the wheels onto the Upper Plate carefully 40 Chapter 1 About the PiCar S ...

Page 45: ...SunFounder PiCar S Then put the assembled Front Half Chassis onto the Upper Plate with standoffs aligned with the holes 1 6 Servo Configuration 41 ...

Page 46: ...SunFounder PiCar S Hold them carefully turn upside down and fasten the standoffs and the Upper Plate with four M3x8 screws 42 Chapter 1 About the PiCar S ...

Page 47: ...the commands to adjust the servo to 90 degrees previously Now let s talk about the other two commands The second command front wheel test is used to test whether the front wheels can turn flexibly after assembly When you run this command it will drive them to turn left and right picar front wheel test 1 7 Calibration 43 ...

Page 48: ...e is 0 by default If you want to make the front wheels turn right a bit just modify it to a larger number to make it more towards the left you can set it smaller it can even be a negative number But DO NOT over configure the wheels recommended a value between 30 and 30 or the servo may be stuck and broken After changing the value of turning_offset press Ctrl O to save the changes and press Ctrl X ...

Page 49: ...he other does backwards If so we need to adjust one or both two wheels which rotate reversely under that command cd home pi SunFounder_PiCar picar sudo nano config forward_A and forward_B are to change the default spinning direction of the two motors The value can only be 0 or 1 which represents clockwise and counterclockwise rotation By default it s 0 for both parameters Thus if a wheel spins rev...

Page 50: ...ility to collect and process the data The sensor modules to the PiCar is what the cartridges to the game console they are added to the basic design of the game and thus richening the play It s also similar to the code The processor will use SunFounder_PiCar to drive the car s movement and call the corresponding code package for different modules SunFounder_Light_Follower SunFounder_Line_Follower S...

Page 51: ...Step 1 Assembly Connect the ultrasonic module to the ultrasonic connector with M1 4 8 screws and M1 4 nuts Then connect them to the ultrasonic support with M3 10 screws and M3 nuts Finally assemble them to the Upper Plate with M3 10 screws and M3 nuts Reminder It would be easier to place the nuts into the slots with your fingers to hold underneath Step 2 Wiring Connect the ultrasonic obstacle avoi...

Page 52: ... range of about 30 40 degrees Thus the distance measured may be not so accurate but that small range provides convenience for obstacle avoidance Besides since the Raspberry Pi is not a real time operating system the inaccurate time calculation will affect the accuracy of distance measurement too However this ultrasonic module is precise enough for obstacle avoidance Step 4 Get on the road Now we h...

Page 53: ... ground It will follow the program to turn when it detects an obstacle if the obstacle is too close it will move backwards and turn left right You can also modify the threshold of obstacle detecting range and that of moving backwards in the code Code Explanation for ultra_sonic_avoid py Whole Work Flow 1 8 Arming the Car 49 ...

Page 54: ...an be converted to the distance to the obstacle Thus we call the time module in Python for timing here The formula to calculate the distance is written in the ultrasonic module s driver The main program just calls the corresponding program to get the distance value Subflow of the Obstacle Avoidance Function 50 Chapter 1 About the PiCar S ...

Page 55: ... take actions Here are three cases when the distance to the obstacle is equals to the threshold the car will turn directions when the distance is less than the threshold the car will move backwards before turning direction when the distance is more than the threshold it will keep moving forwards 1 8 Arming the Car 51 ...

Page 56: ...rning_angle and the angle can be a positive or negative number for turning left or turning right respec tively NOTE that the number of the turning angle should be 90 to 90 considering the servo s max rotation degrees or the servo may be burnt when the detected distance is greater than the turn_distance the car will keep moving forward bw backward making the rear wheels rotate backwards bw forward ...

Page 57: ...nd transfers the data to the processor The processor analyzes the data and finds the direction of the light resource so it will send a command to control the movement of the front and rear wheels to approach the resource Procedures Step 1 Assembly Connect the light follower to the Sensor Connector with M3 10 screws and M3 nuts and then assemble them to the car with two M3 10 screws and two M3 nuts...

Page 58: ...y 3 3V Therefore if the voltage is between 3 3V 5V the output value will always be 255 thus the PCA8591 may be damaged if connected to 5V Remember to connect to 3 3V The wiring is shown as below Step 3 Test Let s test the light follower first cd SunFounder_PiCar S example python3 test_light_module py Expose the phototransistors to the light spot of the flashlight When you increase the light intens...

Page 59: ...p 4 Get on the road git submodule update init python3 light_follower py The car will enter the light following configuration mode when we run the code above It will keep turning to the right in a circle to gather the information of light condition in different directions So just place the car in an open field and wait When the calibration is done the car will stop temporarily Shine a flashlight on...

Page 60: ...sitive sensors need to be calibrated before actual use because of complex light conditions in the environment It gathers the information of the ambient light luminance The car can follow light only when the light source is brighter than the surroundings 56 Chapter 1 About the PiCar S ...

Page 61: ...lues in the brightest conditions as reference Besides we should set a threshold value when the gap between the collected value of the light source and that of the environment is beyond the threshold trigger the value switching to 0 or 1 Here we use 0 0 0 to represent the three photoresistors status when they are not triggered 0 will become 1 when the value detected of the corresponding photoresist...

Page 62: ...for none For example 1 0 0 shows that light is detected only by the left probe meaning th light source is at the left of the car thus setting the car s response action as turning left 1 1 0 means that light is detected on the left and central probes thus its response action should be set turning left too and set it as turning right the same way according to the corresponding status When there is n...

Page 63: ...rs for this kit respectively for light following front wheels and rear wheels The related classes have been defined here When the modules are applied to use objects will be created for related classes and different parts of hardware will be driven by calling a function by the class object For example for the light following module we create an object named lf lf Light_Follower Light_Follower Then ...

Page 64: ...oes out quickly or changes luminance by ON OFF ratio fw turn turning_angle Function for front wheels steering The main program will call this function if the front wheels are applied for steering The parameter is the turning angle bw forward bw set_speed forward_speed Here we need two functions for rear wheels The first function is to control the rotating direction as forward the function for back...

Page 65: ...der_PiCar S example Check whether any i2c device is recognized or not via i2c tools sudo i2cdetect y 1 We can see 11 is the line follower s i2c address If it is not shown it proves your wiring is not correct and the i2c communication with Raspberry Pi fails too You need to check the wiring before the next step Run the test code python3 test_line_module py 1 8 Arming the Car 61 ...

Page 66: ...ace all the probes above the black lines And the prompt of calibration completed will be printed on the screen a few seconds later When the module calibration is all completed we can run the car then Place the PiCar S with probes above the black line on the white board and then it will go forward following the line itself How to make a track for line following To make a track for the car to follow...

Page 67: ...the front wheels and rear wheels The car won t be able to turn and pass the curve smoothly if the semidiameter of the curve is too small A track sample is shown as below the original map file can be found under folder map in github Code Explanation of line_follower py Whole Work Flow Considering the interference of negative environment factors we need to calibrate the line follower sensor before a...

Page 68: ...guration we will start from white color then black color which is more like the upper limit and lower limit of the sensor Then we take the average value of black and white as reference value if the detected value is higher than the reference it should be white if the detected is lower than the reference it should be black We will show the five detectors status by 5 elements 0 0 0 0 0 64 Chapter 1 ...

Page 69: ...SunFounder PiCar S Subflow of Line Following Function 1 8 Arming the Car 65 ...

Page 70: ...e car will turn a large angle Thus here we set four angle turning constants a_step b_step c_step and d_step When the car moves forward originally the servo is in 90 degrees To drive the car to turn left the servo should be in 90 step degrees to turn right the servo should be in 90 step degrees There is a special case if the car runs off the track and all the probes cannot detect the black lines an...

Page 71: ..._wheels They are the drivers for this kit respectively for line following front wheels and rear wheels The related classes have been defined here When the modules are applied to use objects will be created for related classes and different parts of hardware will be driven by calling a function by the class object Similar to the line following module we create an object named lf lf Line_Follower_mo...

Page 72: ...ck below the program of this example Assemble the light follower module and ultrasonic obstacle avoidance module on the car first Log into the Raspberry Pi on your computer via ssh and get into the directory cd SunFounder_PiCar S example Run the code python3 light_with_obsavoidance py How it works Set the obstacle avoidance as a superior priority than light following if there is an obstacle in fro...

Page 73: ...SunFounder PiCar S The main program main will run the corresponding program according to avoid_flag and light_flag and the avoid_flag is superior in priority 1 8 Arming the Car 69 ...

Page 74: ...SunFounder PiCar S 70 Chapter 1 About the PiCar S ...

Page 75: ...cd SunFounder_PiCar python3 setup py install 4 Enable I2C Edit the file boot config txt sudo nano boot config txt The in front of each line is to comment the following contents which does not take effect in a sketch The I2C configuration part is commented by default too Add the following code at the end of the file or delete the pound mark at the beginning of related line either way will do dtpara...

Page 76: ...PIO ports Thanks to the design of the ideal diode based on the rules of HATS it can supply the Raspberry Pi via both the USB cable and the DC port thus protecting it from damaging the TF card caused by batteries running out of power The PCF8591 is used as the ADC chip with I2C communication and the address 0x48 1 Digital ports 3 wire digital sensor ports signal voltage 3 3V VCC voltage 3 3V 72 Cha...

Page 77: ... voltage 2 indicators on 7 9V 1 indicator on 7 9V 7 4V no indicator on 7 4V To protect the batteries you re recommended to take them out for charge when there is no indicator on The power indicators depend on the voltage measured by the simple comparator circuit the detected voltage may be lower than normal depending on loads so it is just for reference 9 Power port 5 5 2 1mm standard DC port inpu...

Page 78: ...irectly 2 3 I2C port 4 wire I2C port can be used in parallel Compatible with 3 3V 5 5V 3 PWM power input 12V max 4 LED power indicator for the chip and for the PWM power input Motor Driver Module The Motor Driver module is a low heat generation one and small packaged motor drive 74 Chapter 1 About the PiCar S ...

Page 79: ...l port includes pins for supplying the chip and the motors and controlling the motors direction 2 PWM input for the motors PWM signal input for adjusting the speed of the two motors 3 Motor output port output port for two motors 1 9 Appendix 75 ...

Page 80: ...exists an object or not by the light intensity It can easily identify black and white lines In other words the different conduction levels of the phototransistor when it passes over black and white lines can generate different output voltages Therefore all we need to do is to collect data by the AD converter on the Atmega328 and then send the data to the master control board via I2C communication ...

Page 81: ...he resistance of the resistor inside changes with the infrared light received Light Follower Module Phototransistor also known as photodiode is a device that converts light to current Currents are generated when photons are absorbed in the P N junction When a reverse voltage is applied the reverse current in the device will 1 9 Appendix 77 ...

Page 82: ...d obstacles With the two holes it can be easily mounted to the robot A four foot anti backwards cable is included to make the wiring tighter and easier The HC SR04 ultrasonic distance sensor provides non contact measurement from 2cm to 400cm with a range accuracy of 3mm each HC SR04 module includes an ultrasonic transmitter a receiver and a control circuit so we have to be careful with the Trig an...

Page 83: ...80 degree three wire digital servo It utilizes PWM signal of 60Hz and has no physical limit only control by internal software to 180 degrees at most Electrical Specifications DC Gear Motor It s a DC motor with a speed reducing gear train See the parameters below 1 9 Appendix 79 ...

Page 84: ...ganization that uses these for commercial profit without permission the Company reserves the right to take legal action 1 10 Thank You Thanks to the evaluators who evaluated our products the veterans who provided suggestions for the tutorial and the users who have been following and supporting us Your valuable suggestions to us are our motivation to provide better products Particular Thanks Len Da...

Page 85: ... it for personal study investigation enjoyment or other non commercial or nonprofit purposes under therelated regulations and copyrights laws without infringing the legal rights of the author and relevant right holders For any individual or organization that uses these for commercial profit without permission the Company reserves the right to take legal action 81 ...

Reviews: