# Modifying python code for Picar-V

**URL:** <https://forum.sunfounder.com/t/modifying-python-code-for-picar-v/158>\
**Category:** General\
**Created:** [March 3, 2023, 1:57am UTC](https://forum.sunfounder.com/t/modifying-python-code-for-picar-v/158 "2023-03-03T01:57:33Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![faja1130](https://avatars.discourse-cdn.com/v4/letter/f/4bbf92/32.png) [@faja1130](https://forum.sunfounder.com/u/faja1130)\
**Post date:** [March 3, 2023, 1:57am UTC](https://forum.sunfounder.com/t/modifying-python-code-for-picar-v/158/1 "2023-03-03T01:57:33Z")

</div>

I have installed the picar-V (via github) software onto a raspberry pi 4 and setup a startup script for the django server in etc/rc.local on bootup. I want to make some enhancements to the python code but am unsure what source code to update. The \*.py and \*.pyc files seem to be scattered across multiple directories on the raspberry pi. So I am asking how to make python programing modifications and re-deploy. I am confused as to the multiple locations and copies of the various files.

Thank You

---

<div class="post-metadata">

**Author:** ![SunFounder\_Moderator](https://avatars.discourse-cdn.com/v4/letter/s/34f0e0/32.png) [@SunFounder\_Moderator](https://forum.sunfounder.com/u/SunFounder_Moderator)\
**Post date:** [March 3, 2023, 10:44am UTC](https://forum.sunfounder.com/t/modifying-python-code-for-picar-v/158/2 "2023-03-03T10:44:51Z")

</div>

faja1130,

If you want to modify the content of the remote control part, you can modify  
~/SunFounder\_PiCar-V/remote\_control/remote\_control/

---

<div class="post-metadata">

**Author:** ![faja1130](https://avatars.discourse-cdn.com/v4/letter/f/4bbf92/32.png) [@faja1130](https://forum.sunfounder.com/u/faja1130)\
**Post date:** [March 5, 2023, 12:39pm UTC](https://forum.sunfounder.com/t/modifying-python-code-for-picar-v/158/4 "2023-03-05T12:39:30Z")

</div>

I want to add functions like sensors and I2C devices. I am trying to do this by modifying ~/SunFounder\_PiCar-V/client/Client.py. Is this the correct place?

There are copies of the client.py code scattered everywhere:

/usr/lib/python…  
/usr/local/lib/python…  
(And many more places)

When I modify SunFounder\_PiCar-V/client/Client.py, it only works running in the python IDE (Thonny) and not when run as a service. I believe the actual binary is executed from some other place.

I have also checked $PATH and nothing referenced there.

Thanks

---

<div class="post-metadata">

**Author:** ![SunFounder\_Moderator](https://avatars.discourse-cdn.com/v4/letter/s/34f0e0/32.png) [@SunFounder\_Moderator](https://forum.sunfounder.com/u/SunFounder_Moderator)\
**Post date:** [March 7, 2023, 10:23am UTC](https://forum.sunfounder.com/t/modifying-python-code-for-picar-v/158/5 "2023-03-07T10:23:06Z")

</div>

To add sensors, the I2C device should be changed in ~/SunFounder\_PiCar-V/remote\_control/remote\_control/. You can see that the run function in views.py will run different functions under different actions. The code inside client.py is to do the client, that is, the control page, it is not recommended to implement the sensor function on this.

---

<div class="post-metadata">

**Author:** ![faja1130](https://avatars.discourse-cdn.com/v4/letter/f/4bbf92/32.png) [@faja1130](https://forum.sunfounder.com/u/faja1130)\
**Post date:** [March 12, 2023, 1:27pm UTC](https://forum.sunfounder.com/t/modifying-python-code-for-picar-v/158/6 "2023-03-12T13:27:40Z")

</div>

OK, I did a simple test by changing the camdown action to cam.turn\_up and it worked. I understand to update the views.py to add/change actions. But if I want to write a new subroutine action and use it, or modify an existing subroutine, where do I put that NEW/Mod subroutine? Do I look in the ./driver folder?

Thank You

---

<div class="post-metadata">

**Author:** ![SunFounder\_Moderator](https://avatars.discourse-cdn.com/v4/letter/s/34f0e0/32.png) [@SunFounder\_Moderator](https://forum.sunfounder.com/u/SunFounder_Moderator)\
**Post date:** [March 14, 2023, 3:33am UTC](https://forum.sunfounder.com/t/modifying-python-code-for-picar-v/158/7 "2023-03-14T03:33:53Z")

</div>

Not quite understand what subroutine refers to, can you give me an example? /driver is a library, that is, some relatively complex operations are abstracted out of a library and put into the driver, in order to simplify the code of the views.

---

<div class="post-metadata">

**Author:** ![faja1130](https://avatars.discourse-cdn.com/v4/letter/f/4bbf92/32.png) [@faja1130](https://forum.sunfounder.com/u/faja1130)\
**Post date:** [March 16, 2023, 12:04am UTC](https://forum.sunfounder.com/t/modifying-python-code-for-picar-v/158/8 "2023-03-16T00:04:35Z")

</div>

I’m sorry, I wasn’t using the correct Python term. I meant writing a new class object (instead of ‘subroutine’) , like you did with the camera.py. I think I have it figured out. Thank You!!!

---

<div class="post-metadata">

**Author:** ![faja1130](https://avatars.discourse-cdn.com/v4/letter/f/4bbf92/32.png) [@faja1130](https://forum.sunfounder.com/u/faja1130)\
**Post date:** [March 30, 2023, 7:05pm UTC](https://forum.sunfounder.com/t/modifying-python-code-for-picar-v/158/9 "2023-03-30T19:05:46Z")

</div>

If I want to add another keystroke function, should that be added to the keyPressEvent in /home/pi/SunFounder\_PiCar-V/client.py??

---

<div class="post-metadata">

**Author:** ![SunFounder\_Moderator](https://avatars.discourse-cdn.com/v4/letter/s/34f0e0/32.png) [@SunFounder\_Moderator](https://forum.sunfounder.com/u/SunFounder_Moderator)\
**Post date:** [March 31, 2023, 10:45am UTC](https://forum.sunfounder.com/t/modifying-python-code-for-picar-v/158/10 "2023-03-31T10:45:35Z")

</div>

If you want to add a keypress function, you can add it to /home/pi/SunFounder\_PiCar-V/client.py in the keyPressEvent.  
The code can only be modified on the Raspberry Pi system terminal.

---

<div class="post-metadata">

**Author:** ![faja1130](https://avatars.discourse-cdn.com/v4/letter/f/4bbf92/32.png) [@faja1130](https://forum.sunfounder.com/u/faja1130)\
**Post date:** [March 31, 2023, 1:32pm UTC](https://forum.sunfounder.com/t/modifying-python-code-for-picar-v/158/11 "2023-03-31T13:32:57Z")

</div>

Thank you for the reply. In previous comments I was told to also make changes in ~/SunFounder\_PiCar-V/remote\_control/remote\_control/ in views.py for additional function. I have done that successfully. I used a text editor or Thonny python to make those edits.

But this time you say code can only be modifed on the Raspberry Pi system terminal. I don’t understand what you mean…???

I just used a text editor, but the changes are not taking effect.  
When I change the VIEWS.py file, the changes are immediate, but changes to CLIENT.py are not. Is there an additional step required? I also rebooted the pi after my changes.

[[ I should also mention:  
1) I have /home/pi/SunFounder\_PiCar-V/remote\_control/start script running automatically at raspberry pi boot.  
2) I am using method B to invoke the client - http://:8000 ]]

Thanks

---

<div class="post-metadata">

**Author:** ![SunFounder\_Moderator](https://avatars.discourse-cdn.com/v4/letter/s/34f0e0/32.png) [@SunFounder\_Moderator](https://forum.sunfounder.com/u/SunFounder_Moderator)\
**Post date:** [April 3, 2023, 9:01am UTC](https://forum.sunfounder.com/t/modifying-python-code-for-picar-v/158/12 "2023-04-03T09:01:18Z")

</div>

code can only be modifed on the Raspberry Pi system terminal  
It means that you need to modify the views.py on the Raspberry Pi to change the views.py not on your computer.

The http::8000 you are accessing is the web client and the code is in [SunFounder\_PiCar-V/remote\_control/remote\_control at master · sunfounder/SunFounder\_PiCar-V · GitHub](https://github.com/sunfounder/SunFounder_PiCar-V/tree/master/remote_control/remote_control/) templates/templates is in html because it is a web page.

The client.py is a separate pyqt program and has nothing to do with the server side. after the client.py code is modified, just close the client.py and run it again.

---

<div class="post-metadata">

**Author:** ![faja1130](https://avatars.discourse-cdn.com/v4/letter/f/4bbf92/32.png) [@faja1130](https://forum.sunfounder.com/u/faja1130)\
**Post date:** [April 3, 2023, 12:42pm UTC](https://forum.sunfounder.com/t/modifying-python-code-for-picar-v/158/13 "2023-04-03T12:42:04Z")

</div>

I am not running the Client.py on the PC. I only use the PC for the Web Browser to access http:8000.  
Now I understand your comment about using Raspberry Pi system terminal. (If I was using the PC for the client).

So when strictly using the server (running on the Raspberry Pi), I would need to update the Run.html in template to add another key function.

Thank You for your help thus far!

---

<div class="post-metadata">

**Author:** ![faja1130](https://avatars.discourse-cdn.com/v4/letter/f/4bbf92/32.png) [@faja1130](https://forum.sunfounder.com/u/faja1130)\
**Post date:** [April 17, 2023, 9:41pm UTC](https://forum.sunfounder.com/t/modifying-python-code-for-picar-v/158/14 "2023-04-17T21:41:01Z")

</div>

I would like to add data to the running\_screen.ui. For example provide some real-time feedback to the webpage. If I have an ultrasonic range sensor, how could I display the current range from an object on the webpage?

---

<div class="post-metadata">

**Author:** ![SunFounder\_Moderator](https://avatars.discourse-cdn.com/v4/letter/s/34f0e0/32.png) [@SunFounder\_Moderator](https://forum.sunfounder.com/u/SunFounder_Moderator)\
**Post date:** [April 18, 2023, 10:37am UTC](https://forum.sunfounder.com/t/modifying-python-code-for-picar-v/158/15 "2023-04-18T10:37:46Z")

</div>

Our engineers are already working on your problem and it will take some time to get back to you.

---

<div class="post-metadata">

**Author:** ![SunFounder\_Moderator](https://avatars.discourse-cdn.com/v4/letter/s/34f0e0/32.png) [@SunFounder\_Moderator](https://forum.sunfounder.com/u/SunFounder_Moderator)\
**Post date:** [April 20, 2023, 8:47am UTC](https://forum.sunfounder.com/t/modifying-python-code-for-picar-v/158/16 "2023-04-20T08:47:31Z")

</div>

As I said before, the code for the web client is in the remote\_control/remote\_control, not the client folder.

You will also need knowledge of simple html, and use of the django library if you want to add some live data from sensors. The process is as follows:

1. You need to add a function to get the ultrasonic data in the remote\_control/remote\_control/views.py file, written with reference to the run function, return returns the ultrasonic reading value
2. in remote\_control/remote\_control/urls.py  
You need to modify the `remote_control/remote_control/templates/templates/run.html` file to add a tag that displays the data. Then add a timer under the script, and every once in a while, through the interface written above, get the ultrasound data and assign it to the tag that displays the data on the page.
