User avatar
JonnyAlpha
Raspberry Pi Certified Educator
Raspberry Pi Certified Educator
Posts: 564
Joined: Sat Nov 02, 2013 2:06 pm

Program management - modules and functions

Sat Nov 03, 2018 10:09 am

Hi;

I am working on a robotic vision project and need a little help in managing the different elements of the program.
The first phase is to navigate a maze.
We now have a basic working vision element that can identify the edges of the walls and which direction to turn in based on the angle of the wall in relationship to the robots position. We also have a working program operate the motors, this is a MonsterBorg based robot using a ThunderBorg Motor Driver and there are plenty of examples of code around.

What I want to do now is to put the two programs together so that the code that now activates the motors using information from the image processing element of the program.

What is the best way to manage large complex programs? Option 1 - should I have one large program that contains all the various functions returning data between them or Option 2 should I use separate programs and import the separate elements of the program using modules?

Option 1

Code: Select all

import cv2
def image_processing():
	.....
	.....
	.....

def driving():
	.....
	.....
	.....

def main():
	image_processing()
	driving()

main()

Option B

Code: Select all

From Image_Processing import image_processing

def main():
Insert the driving code here using data imported from the image processing module
I have an example of the second type of arrangement which uses classes but it is a little confusing in how it all knits together?

Any pointers to guides on program structure would help :-)

Forgive my lack of knowledge but programming is not my main hobby / work :-)
Raspberry Pi Certified Educator. Main Hardware - Raspberry Pi 1 model B revision 2, Raspberry Pi 2 model B, Pi Camera

zappfinger
Posts: 34
Joined: Tue Nov 26, 2013 7:25 pm

Re: Program management - modules and functions

Sat Nov 03, 2018 11:58 am

I'd say, keep the functions separated in classes. And in the main program import and use these classes.
Concentratie in doing one thing only in a class (e.g. 'driving' ). Test the classes in their own __main__ section.
Maybe you can use the classes in another project as well then.

Richard

User avatar
JonnyAlpha
Raspberry Pi Certified Educator
Raspberry Pi Certified Educator
Posts: 564
Joined: Sat Nov 02, 2013 2:06 pm

Re: Program management - modules and functions

Sat Nov 03, 2018 1:04 pm

Thanks Richard, I've never used classes or this method before but it does seem a great way of keeping things modular.
Raspberry Pi Certified Educator. Main Hardware - Raspberry Pi 1 model B revision 2, Raspberry Pi 2 model B, Pi Camera

Return to “Python”