Coding in Python

For someone who studied pharmacy for 5 years and work in bank for 13 years, programming and coding with Python is definitely outside of my comfort zone and totally a different “language” that I’m not familiar with. I always want to learn another language, but I never prioritised this given my busy schedule with working full-time, juggling young family life plus studying part-time. I never thought that I will be learning programming language such as Python (not python the snake!). From the very first week of the program, I was introduced to coding using Python language with the help of a pen plotter (a drawing robot) called Harry the Plotter using Python Turtle and Visual Studio Code.

Me and python the snake
Python Turtle
VS Code

I believe learning coding in Python can be done through multiple way and below is how I learned so far:

  1. By throwing myself into the deep end. Each fortnight, I always try to do my best in participating every build skill and studio session and completing each given homework. I believe the teaching staff planned all these activities and homework to help us learning by doing and practice makes perfect! I know it can be overwhelming for some people to learn so many things in such a short time. It’s hard sometimes to reflect and digest what I’ve just learned without understanding the basic concept or having a proper introduction. Before I know, I have to move on to the next big thing to learn, and repeat for every week/fortnight! But I always believe in continuous learning and keep challenging myself to learn new things in life. I will learn more when I feel like being challenged and stretch myself to be outside of my comfort zone. I’d like to turn this scary feeling into a productive discomfort.
  2. By learning from tutors (Johan, Mina, Matthew and Memunat) and other cohort members in doing the homework together and collaborate on the Maker Project.
  3. By interrogating and questioning other people’s code, for me it started with the chatbot called ELIZA when we were introduced to her in class. It was hilarious and frustrating at the same time when I was pair up with Julian to play the Eliza and the other as the chatbot user. Eliza is a computer program for the study of natural language communication between man and machine, developed by Joseph Weizenbaum in 1966.
  4. By joining the Studio Tutorial session with Zac, thank you so much for your patient and willingness to help us this year.
  5. By watching online video tutorials in YouTube or other channels and reading the resources available in the Studio. Also to understand the different tools/program that could be used to run Python. I know some people prefer VS Code, Mu editor, Thonny or other editors. I found a little bit confusing and overwhelming at the beginning, So my take-away is to try one by following the instruction on how to, then go with what I feel the most comfortable or familiar as my preference. For me I like working with Mu editor because it feels simpler and I’ve been using it for my Maker Project and few homework. But I know I need to learn and familiar myself with other editor program if I plan to continue my Python journey for future work (depends on what program they will use I suppose).
  6. By attending (joining online) the PyConline AU 2021 during the mid-semester 2 break (10-12 September 2021).

Overall, based on the Dreyfus Model of Skill Acquisition, and through many Build fortnightly homework, Build tutorial and skill session and Maker Project, I would categorise my current understanding of Coding in Python as Competent level now, moving from having zero experience (Novice level) before pre-master. I know that the best way to improve for me is to continue and practice using it more often and to observe other people while working with python to solve problems. I hope that my proficiency will continue to improve over Semester 2 as I complete more Build fortnightly homework and build a Cyber Physical System for our CPS project group. My desired skill level will be Competent to Proficient and I’m planning to achieve this level by the end of this year through building some electronic toys/robot for my kids using Python codes and learning from online tutorials. Please see the chart below for an overview of my Coding in Python learning progress.

An overview of my learning progress in Coding in Python

Below are some examples of my skill development that highlights the successes and failures, lessons learned and turning moments.

A Tale of Two Chatbots

Our first task was to write a line of code to draw letter ‘A’. Just like learning a new “language”, understand the alphabet is definitely a good start.

(Please click on each letter/picture to see the codes)

My very first coding in Python drawing letter “A” using a pen plotter robot Harry the Plotter

I was given two letters “C” and “D” as a challenge to put my spatial reasoning to the test, followed by the repeated in the reverse order as “CDDC”. Then I challenged myself to make it curve, and it was more complicated because of the circular rotation. However, after few trials and error attempts, with simple tips from Johan to always bring back the position to home, it finally worked! The next challenge was harder as we had to create a pyramid using for loops function in Python codes with the given letters earlier and pyramid level based on our names alphabet length. So pyramid “C” 5 levels (Myrna = 5 letters) and pyramid “D” 7 levels (Kennedy = 7 letters).

“CD”
Straight CDDC
Curve CDDC
Straight “C” pyramid
Curve “C” pyramid
Straight “D” pyramid
Curve “D” pyramid

Proud Pyramid Yippee Ki Yay Moment

I learned that for loops function can iterate over a given sequence. To create the pyramid I used two simple for loops functions using level as the range. After few trial and error of multiple functions combination, I found the formula below, set up all the parameters and made assumptions for X and Y coordinates. I also created a matrix table and plotted those number coordinates on graph paper to help me visualising the pyramid better. The height and width of the letter can be adjusted to make it look nicer with the right proportion for the pyramid.

Width * (b – a) * 2

Height * (a + b)

Using my new skills in coding, I was so proud to produce these amazing “arts” including the accidental arts that were created from my unintended outputs which apparently contributed to the Harry the Plotter Hall of Fame!

I believe all these tasks were designed in a way to guide me in stretching my new skill one step further beyond what I thought I could do. I like being challenged because it pushes my limit to the next level and I hope this can help my learning goals throughout my study in Master of Applied Cybernetics this year.

Often I learned the hard way from my curiosity and trial-error attempt, but this helped me to learn that just because something works well, it doesn’t always translate to the right thing. Particularly in building Cyber-Physical Systems (CPS) when we are working with robot (the hardware) and the programming code (software), how can we assure that the robot will behave as intended, inclusive and fair. As the New Branch of Engineering (NBE) practitioner to be, I would like to make sure what I build is safe, responsible and sustainable in the future.

Tic Tac Toe

The fortnight 2 homework topic was Purposeful Behaviour and we explored the concept of algorithms and building our programming skills to create a an agent, and challenge our agent to play the Tic Tac Toe game against ourselves. Using pen plotter, our agent will make its next move based on the logic of the underlying agent that we programmed using Python. In the Build studio skill session we were introduced to the ‘journey’ of coding, and how to progress from idea to strategy, to algorithm to pseudo-code to code. I found the skills session to play a game of Tic Tac Toe was so much fun, interesting engaging and yet challenging at the same time as my skill level in coding in Python after the first fortnight was still at novice level. James explained his logic of listing all the position and possible moves in the index form. His explanation has helped me to update my agent code and able to play winning game against myself.

For me the journey from idea to code has been really helpful to guide my thinking process in particular the transformation process from pseudo-code to code as my focus area how to “bridge” these two building blocks. In computer science, informal algorithm definition is a set of rules that precisely defines a sequence of operations. The pseudocode is a plain language description of the steps in an algorithm or another system. Pseudocode often uses structural conventions of a standard programming language but is intended for human reading rather than machine reading, which I believe if we get the pseudo-code correct. Hopefully it will translate into the right code as we intended to be, something that I uncover more through the Cybernetics lens this year.

The Component:

  • The AxiDraw or pen plotter, called “Harry the Plotter”.
  • A camera and computer vision system that works with Raspberry Pi to recognise the symbols played and the location on the playing board.
  • A playing board A5 size
  • Colourful whiteboard markers
  • USB stick to save your agent code

The Process:

First, you need to set up the AxiDraw or pen plotter by following the step by step instruction below written by Memunat and updated by Johan.

Then I tried to code and create the winning agent by using the if statement and identified the 8 possible winning positions per the engine.py file. The result showed that my agent wins more than losses, so I was pretty happy with the output. But when I tested my code by playing a game against my agent, I won, and my agent lost on the first game. I should be happy, but I was disappointed to see how my agent behaviour was not expected based on the codes that I thought I wrote the strategy correctly. So, I tried to implement a different strategy into my code, and I played the second game against my agent. I was so surprised that my agent can beat me but not only winning the game but also blocking my winning move, so happy days! 

AxiDraw or Pen Plotter with the camera attached on the arm

First game:

It’s really interesting to see the difference between what I thought in my mind when I wrote the code with the way of the Tic Tac Toe system actually worked on the plotter machine. On the first game my agent (“X”) lost against me (“O”). My agent made the first move on position 2, follow by me on position 4 and then my agent next move on position 0, so I block it from winning by placing my circle on position 1. Now interestingly, my agent should block me from winning by going to position 7, but instead my agent went to position 6. So, I made my winning move on position 7. I then hit another enter to see if my agent will move to its winning move which should be on position 3, but instead my agent went to position 8 haha… I need to fix the strategy for my agent to take winning move first if possible then follow by blocking the opponent from winning as the next priority.

Second game:

Learning from the first game’s mistakes, I modified my code to see if my agent can beat me this time. Again, my agent made the first move on position 2, follow by me on position 0 and then my agent next move on position 6, so I block it from winning by placing my circle on position 4. Interestingly, my agent now knows how to block me from winning by going to position 8 yay! And to block my agent from winning the game, I place my circle on position 7. Now my agent has two choice, either make the winning move on position 5, or block me from winning by moving to position 1. And my agent moved to position 5 to win the game! So happy to see that the strategy for my agent to take winning move first if possible then follow by blocking the opponent from winning as the next priority is working on this second game.

3 x 3 winning agent

Note: My agent always making the first move and always playing as “X”.

Troubleshooting: For drawing the circle, if the camera is struggling to recognise the circle drawing (most of the times in my case!), try to use a thicker marker or colour in the circle to make it recognisable by the camera/sensor.

Below are the Algorithms & Pseudocodes that I’ve done for this fortnight homework. For the actual codes that I used, please click the blue link.

3×3 Tic Tac Toe – Kennedy Agent

I selected the “Impress Me” for the stretch task by making the Tic Tac Toe board more challenging, interesting and fun. So, I changed the board from 3×3 squares to 4×4 squares instead. This idea was inspired by my son, who likes to play 3×3 Rubik’s cube, and now he’s exploring the 4×4, which he found harder.
To change the board, however, I realised I have to update the engine_impress.py file, and the agent will play the game using the engine.py and interact with the Game Engine. Below is the pseudocode for the Tic Tac Toe game using a 4×4 squares board.

4×4 Tic Tac Toe – Impress Agent

4×4 Tic Tac Toe – Engine Impress

4 x 4 “Impress Me” agent

I realised after creating this agent to play 4 x 4 Tic Tac Toe game is just playing too much games and took a long time to process that keep making my laptop crashed/frozen. So my “Impress Me” agent is not that impressive after all.

Reflection

My idea of Tic Tac Toe is a simple game to play but I believe it’s a complicated strategy to code in python. I know the game and the approach to win the game in plain English, but I have no idea how to translate that into python codes. I tried to identify and list the agent, performance measure, environment, actuators and sensors as per the PEAS table. Another method I tried was following the coder’s journey: from idea to code steps which helped me to form my code. Having some discussion with other people also helped me think from other people’s perspective.

To overcome some of the technical challenges, I mainly collaborate with other people and ask for help after give it a go first. I believe these collaboration approach and positive attitude may inform my skill and capability development in the future. The difference is I know one-fortnight worth of python on the second homework, so more familiar with the concept.

I learned there are no system that is perfect and it can have some unexpected and unintended behaviour or outcome. I also learned to fail inevitably as I learn by doing many iteration, trial and error when I work with Python codes and the machine. Interacting with the cyber physical system for the first time and exposed to “cool” technology was definitely my highlight this fortnight, and I feel so proud and have a sense of accomplishment when I can make the machine to move with the codes that I programmed. totally a different perspective when I’m so used to be on the technology user side and now I’m on the other side of creating and designing the technology. it makes me think differently to improve the user experience in future and be more inclusive, aligning with the 3AI mission to build AI-powered CPS that is more responsible, sustainable and safer.

I always approach any challenges that I face with enthusiasm and curiosity. I think I’m considered a fast learner and my rapid journey of learning how to write code and translate ideas into machine languages this fortnight has proven this. However with regards to the reflective practice and learning journey so far, I found it was hard to reflect on what I’ve done or learnt. I often reflect but never put it on writing, just on my thoughts. And after the very busy first fortnight I didn’t have a chance to write down on how did I go and reflect on what I’ve done so far. I had to try harder to remember what I’ve done on that fortnight. It is difficult to make this as a habit but I know I should learn and start to do this from now on as it is a good habit to develop while I’m learning something new and going through this journey of becoming the NBE practitioner in future.

Acknowledgement

Memunat and Matthew who helped us in the class how to consider the logic for our agent codes.

James who explained his logic of listing all the position and possible moves in the index form, this helped me to update my agent code and able to win against me.

TurtleBot

What is a TutleBot? It’s an obstacle avoidance and autonomous navigation robot that looks similar to Roomba vacuum cleaner robot but with computer attach on the top for student learning/research.

Our task in this Build Skill Session is to program the TurtleBot to navigate it through the maze from the start to the end point with only one sensor, to detect if the turtlebot bumped into something or not.

This TurtleBot workshop took me on a another coding in Python journey, from idea –> strategy –> algorithm –> pseudocode and to code. It’s great to see the application for our homework task in creating a Tic Tac Toe agent using the same concept. We were split in two groups: the iterators and the planners. I never program any robot so I was super excited that I get to play with the TurtleBot as the iterator group. Our team (Jake, Chloe, Adrian, Erika and myself) did the split strategy to work out a couple of iterations. With the minimal preparation time, we tweaked our code to escape the maze by repeatedly testing it on the robot, observing the behaviour and improving the code as we go. The first one didn’t quite work as we expected when we tested it because the move in the actual field wasn’t perfectly squared or 90-degree angle each time the sensors bumped as we predicted in our codes. So we updated the codes for our second iteration and tested it again straight away, and it worked! We made the TurtleBot find the way to the end of the maze yay! The TurtleBot is so A-MAZE-ING!

TurtleBot Code
Happy TurtleBot
The A-MAZE-ING TurtleBot find the way to the finish end

The other group (the planner) had longer preparation time, where they could plan and think about different ways to tackle the problem first. They could observe the failures from our group and build upon them. The twist was they can only run their code ONCE on the robot at the end. I was so surprised, that our group (the iterator) were the ones who succeeded to guide the TurtleBot out of the maze. The key learning that I took from this coding exercise is that nothing is perfect. Even though you think you know the code really well, in the reality when you apply the code into a robot (Cyber-Physical System), something could go wrong or not giving you the output as you expected. So I believe it’s important to constantly trying, testing and failing, to observe how the machine executes the codes and calibrate the system as you go to improve the desired outcome.

This task had helped me to understand the concept of purposeful behaviour from the Question course and to realise that system complex behaviour could succeed through simple commands. With using only one sensor (bumping into something or not), our group wrote a simple algorithm to guide the TurtleBot movement based on the sensor input, we were able to successfully navigate the TurtleBot to escape a maze on our first ever day of interacting with a robot, we were so happy and proud of ourselves that day. How a-maze-ing is that!! This excitement feeling made me want to work with a robot and more sensors and to apply them for my Maker Project as I really enjoyed the iteration process from this workshop. The TurtleBot was one of my many inspiration to my Maker Project. For more details on my Maker Project journey in coding with Python, please click the link below.

My Maker Project – Cat RoPot

My Interface – Cereal Dispenser Robot

CPS Project – SMARC

I believe I have progressed from zero skills in Coding in Python to building my maker project which is a cyber-physical system that had so many Python codes to form a 3 in 1 integrated device. Based on my learning experiments and experiences in Semester 1 and 2, I think I am still sitting at the Competent level on the Dreyfus Model. I hope to continue developing my skills in Coding in Python to become Proficient or even Expert in the future after finishing he Masters program and have the opportunity to apply this skill at work.

<Back to Building CPS