LEGO Spike Prime – “Translate” Programming Blocks into Python Code

In my previous post, I introduced how to set a mission for LEGO Spike Prime and implement the task with LEGO Spike programming blocks. As I mentioned before, Lego Spike Prime supports MicroPython pretty well. Most of the programming blocks could find corresponding Python functions.

In this post, I will demonstrate how to “translate” my previous programming blocks into Python code. I will also show an issue encountered when writing Python code and a way to work around it.

The programming blocks and mission setting could be found at this post. Since the programming blocks are long, I will not list them here again.

“main” Function

The whole program just uses the default template of LEGO Spike Python project. It imports all the necessary libraries at the beginning of the file.

Just like the programming blocks shown on the left, the “main” function of Python code contains several functions which divide the whole working process into several sub segments. I will introduce them one by one.

“Start_off” function

The program initiates two colour sensors and one distance sensor at the beginning. In “start_off” function, most of the code has corresponding programming blocks, so it is pretty convenient to “translate” programming blocks into the Python code. MotorPair objects are used to control 2 motors simultaneously in opposite directions. Please note that “wait until” block could not be translated directly since Python has not such kind of loop statement. I need to use “while” loop to replace it and make proper adjustment to make code run well.

“start_line_sensing” Function

The distance sensor sometimes will return None type, which I believe is a bug of the current MicroPython version. To workaround this problem, we need to check if the retrieved value of “get_distance_cm” is equal to None. Otherwise, since None type is different from float type, the system will throw out a runtime error and the whole program will stop.

The Python code here realizes the same functionality as block programming. It uses proportional line following method to control the movement of the motors.

“move_clip” Function

This function is simple. It just drives a single motor which connects to Port F to run clockwise or counter-clockwise for a certain degree.

“send_to_destination” Function

This function will send an object to the destination. The code is similar to the above “start_line_sensing” function. It uses proportional line following method to trace the black line until color sensor on Port E identifies color red, which is the edge of the destination.

“return_to_base” Function

This function returns the vehicle robot to the starting place. However, after using Motor object to control Port B and Port D motor, the program stops unexpectedly. It seems the problem happens when using MovePair and Motor objects to control the same ports. It is a bug of the current MicroPython version.

Continue…

To workaround this problem, I should only use MovePair objects to control the two motors. The following is a method to implement it.

Update of “return_to_base” Function

In the following code, Motor object related code has been replaced with a function called “run_until_touching_red_line”. Except this, all the other code is the same as the previous version. I will explain “run_until_touching_red_line” function below.

“run_til_touching_red_line” Function

This function sets two variables “motor_b_status” and “motor_d_status” which represent the move/stop status of Port B motor and Port D motor. Color sensor E and C are mounted to the side of Port B and Port D motor, respectively. In while loop, when colour sensor E identifies red colour, it will set the “motor_b_status” to 0, meaning that the motor B on its side should stop running.

After that, the program checks the value of “motor_d_status”. If “motor_d_status” is 0, meaning that the other motor has stopped running, the program knows that it could stop both motors by calling motor_pair.stop() and exiting the while loop. If “motor_d_status” is equal to 1, the program will call motor_pair.start_tank(0,30), so that only the motor D on the other side keeps moving.

The same logic applies when colour sensor C identifies red colour. Through using the two variables “motor_b_status” and “motor_d_status”, MotorPair function could specify the speed of each motor correctly.

continue…

That is all the MicroPython code in order to accomplish the same mission done by programming blocks. If you are keen to try it yourself, you could copy the above code into Python canvas and check how it works. Enjoy the coding and have fun!

Note: All the analysis articles are copyright products of http://www.thecodingfun.com. Anyone re-posting them should credit author and original source. Anyone using them for commercial purposes or translating them into other languages should notify TheCodingFun and get confirmation first. All Rights Reserved.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.