MTE 301: Programming for Mechatronics Engineering
Introduction to Programming
Prof. Robin Chhabra
Mechanical, Industrial, and Mechatronics Engineering
Toronto Metropolitan University
Course Overview
Class Hours/Location: Thu 15:00-16:00 (TRS3176), Fri 12:00-14:00 (ENGLG11)
Course Website: my.torontomu.ca (for courses using D2L)
Instructor Name: Dr. Robin Chhabra
Office Location: EPH415
Email Address: robin.chhabra@torontomu.ca
Phone: (416) 979-5000 ext. 554097
Office Hours: By appointment
2
Course Overview
Textbooks:
Boguslaw Cyganek, Introduction to Programming with C++ for Engineers, Wiley-IEEE Press, 2021
Eric Matthes, Python Crash Course, 2nd Edition: A Hands-On, Project-Based Introduction to
Programming, 2019
Course Organization:
3 hours of lecture per week
2 hours of lab/tutorial
per week
3
Course Overview (Evaluation)
Evaluation Component
Percentage of Final Grade
Midterm (2 hours, in class)
Final Exam
Labs
Assignments
Group Project
Total:
20%
25%
15%
15%
25%
100%
achieve a minimum overall grade of 50%,
achieve at least 50% in the weighted combination of the midterm+final, and
achieve at least 50% in the weighted combination of all lab work.
There will be 3 labs (each 5%) and 3 assignments (each 5%)
3% lab attendance and 12% lab reports.
Except where requests for Academic Consideration are granted, late submissions will be penalized at a
rate of 10% per day, where weekends count as 2 days for online submissions.
4
Course Overview (Content)
Introduction to computers and programming
2. Fundamentals of C and C++
3. Classes and Objects
4. Decision Structures
5. Data Structures
6. Inheritance and polymorphism
7. Resource allocation in C++
8. Memory Management
9. Basics of Parallel Programming
10. Introduction to real-time computer programming
11. GUI and Front-end
12. MATLAB and Simulink
13. Introduction to Python, and projects with C/C++ and Python modules
1.
5
Overview
Mechatronics Engineering
Hardware Model
Software Development:
Software Ecosystem
Software Development Steps
Representing and Running Algorithms
Structure of a C++ Program
Building an Executable
Example (Compound Interest)
6
What is Mechatronics?
Mechatronics is a synergistic approach to the design, development and manufacturing of
multidisciplinary engineering systems, where the emphasis is on the physical integration
and information communication amongst various subsystems in a holistic fashion.
Mechatronics is an interdisciplinary field of engineering dealing with the concurrent design
of products whose function relies on the synergistic integration of mechanical and
electronic components coordinated by a computer control structure.
Mechatronics is the synergistic, interdisciplinary field of engineering science that fuses
mechanics, electrical, electronics, informatics and intelligent systems, control systems and
advanced modeling, optics, systems engineering, artificial intelligence, intelligent computer
control, precision engineering, virtualization, and virtual environments into a unified
framework that enhances the design of products and manufacturing processes.
7
Where is Mechatronics?
Mechatronic systems appear in many industries
Automotive: Cars, Trucks, Trains
Aerospace: Airplanes, Drones, Shuttles, Satellites, Space Robots
Medical: MRI, CT Scan, Rehabilitation, Surgical Robotics
Manufacturing: Manipulators, Production Line
Material Processing
Mechatronic systems include:
Control Systems
Computer Systems
Mechanical Systems
Electronic Systems
8
Examples of Mechatronic Systems
9
ELIXIR Lab
Embodied Learning and Intelligence
for eXplorationa and Innovative soft
Robotics
10
Mechatronics Design Challenges
The close interaction and communication of the subsystems necessitate a concurrent approach to
the synthesis of mechatronic systems, where the synergy due to the integration of different
disciplines has been captured. The challenge is to consider a large number of design
variables and multidisciplinary objective and constraint functions and handle a
prohibitively-complicated mechatronic model.
Modeling, simulation and experimentation
Multidisciplinary Design Optimization (MDO) is complex
The mechatronic system designer must be a generalist, willing to seek and apply knowledge
from a broad range of disciplines.
Requires system-level knowledge
Requires knowledge of production process
Requires control & computer system knowledge
11
Mechatronics Engineers
Mechanical design skills
Understanding and design knowledge of electrical/electronic circuits
Strong background in (computer) control systems including:
Microprocessors
Sampling and data processing
Familiarity with computer hardware architecture
Ability to write and debug software to interact with hardware
Skills in developing multidisciplinary simulations
Knowledge of industrial suppliers and limitations
Be able to work in a team of engineers
12
Hardware Model
To create a computer program to solve a given task, we need to specify the operations a
computer must perform (Pseudocode, Algorithm).
The list of operations (instructions) must be written in a form a computer or its processor can
understand (Assembly or Machin Language)
Low-level languages are hard to pars, debug and not efficient for humans.
High-level languages: C/C++, Python, Java, Matlab, etc.
Any programming requires basic knowledge of computer hardware
Computers are composed of dozens of chips each includes thousands of transistors and other
electronic components
Von Neumann Architecture: A simplified computational model of computer with multi-core
processor, main memory, system support chipset, external devices (keyboard, screen, disks,
network device)
13
Hardware Model
14
Hardware Model
15
Hardware Model
Arithmetic Logic Unit (ALU): Heart of the core performing basic low-level operations (binary-
encoded commands stored in memory)
Commands are in the form of machine language that can be represented in assembly language
We code in high-level languages (C++) that can be translated by a compiler to machine language
Core’s Registers: Limited memory locations of ALU operations
Universal Registers
Program Counter (PC): Current position of the executed code
Stack Pointer (SP): Points at external memory (stack) containing return addresses
Zero (Z) and Carry (C) are flags encoded as specific bits in control registers helping with
numerical operations
L1, L2, L3: memory blocks (caches) within a core with different sizes and access times
16
Hardware Model
Arithmetic Logic Unit (ALU): Heart of the core performing basic low-level operations (binary-
encoded commands stored in memory)
Commands are in the form of machine language that can be represented in assembly language
We code in high-level languages (C++) that can be translated by a compiler to machine language
Core’s Registers: Limited memory locations of ALU operations
Universal Registers
Program Counter (PC): Current position of the executed code
Stack Pointer (SP): Points at external memory (stack) containing return addresses
Zero (Z) and Carry (C) are flags encoded as specific bits in control registers helping with
numerical operations
L1, L2, L3: memory blocks (caches) within a core with different sizes and access times
17
Hardware Model
Main (Operational) Memory: External to the cores with longest access time and orders of
magnitude larger; It contains the whole code and data
To access we need:
Address: location of memory cell with smallest cell being a byte (8 bits)
Data: bidirectional bus containing data for read/write to memory with a 32-64 bits long data bus
(computer word)
Caches are copies of the main memory for faster access of ALU. C++ does not have direct access
to them, which is not needed. Nevertheless, they can be efficiently employed in the execution of
C++ programs by proper data placement in the operational memory.
18
Software Development Ecosystem
Without software, computer hardware does not do anything useful
Software must be compatible with the computer hardware (Dichotomy)
Software development ecosystem:
Software design, Algorithm development, and Coding in proper language
Software Build: Compiling (translating) into the machine language; Linking different code modules to
build a final program (Now we have a code executable by a computer)
New development stages: Testing & Debugging
Debug: A software version that usually is not optimized and is used for “in house” testing and debugging
Release: A stable version of the software after intensive testing, which is fully operational, optimized, and intended to be
distributed to users
Profiler: Its role is to measure the performance of software‐building components and discover hot
spots (most execution time) where the performance can be improved by optimizing and parallelism.
During software development, we must protect and control the versions of our software (source
versioning & maintaining). This can be done by one of the online platforms, e.g., Git or GitHub. 19
Software Development Ecosystem
20
Software Development Steps
21
Software Development Steps
Requirements specification – A process of defining the main tasks to be solved with cost and
foreseen deadlines, as well as functionality and applications of the product. Involves cooperation
between many parties, such as end users, system architects, managers, etc.
Problem analysis – The process of establishing the main relations in the system, recognizing the
governing rules of the critical system processes, identifying the most difficult components, and
sketching the main stages of the critical algorithms with focus on the most problematic ones.
Solution design – The process of specifying the architecture of the whole system, considering all
of its components and their interfaces. Includes specification of tools and implementation
strategies. Involves the preparation of Gantt charts that capture interdependencies between
system implementation stages, human resources, deadlines, and milestones.
Implementation – The process of software creation based on the previous steps. The main
product of this stage is the source code based on developed/existing algorithms.
22
Software Development Steps
Testing and debugging – Joint processes of discovering and fixing malfunctioning software. Tests
should be well designed using formal methodologies. However, problems may arise when a
malfunctioning implementation is due to flaws in the design or other upper levels of the
development which require design iterations.
System deployment – The process of system integration and installation to ensure proper
operation on a specifically configured hardware platform. This usually requires adjustments to
specific hardware components.
System maintenance – The practice of ensuring uninterruptable system operation and allowing
for system evolution and seamless system changes.
23
Representing Algorithms
An algorithm describes steps (instructions) for execution. Some actions may not be detailed.
Unified Modeling Language (UML) activity diagram (flow chart): (1) Action; (2) Decision
Pseudo-Code: mixture of computer commands with short human‐readable sentences.
24
Structure of a C++ Program
25
Structure of a C++ Program
A program consists of a series of statements, separated by semicolons ;.
The block of the main function continues inside a region enclosed by braces {} with no ;.
26
Building Executable C++ Programs
comments start with two slash symbols //
#include statements followed by a file name in <> brackets effectively copy and paste the contents of
the indicated file into the current position.
The main function is defined with its body contained between the curly braces and int in front of main
means it is supposed to return a value.
Variable named x of type double (floating‐point approximating real numbers), is defined and initialized
std::cout object to display the text "Enter x=" on the screen
std::cin object and >> operator to enter a value of x from the window
the condition x >= 0.0 is checked with the if‐else statement
std::sqrt( x ) is a call to a function named sqrt, contained in the Standard Library
hence the std:: prefix and the #include <cmath>
What happens if x was negative: known as undefined behavior (UB), which we must avoid.
Scope resolution operator :: for accessing members of namespaces, data and functions, classes, structures
27
Building Executable C++ Programs
Installing Microsoft Visual Studio Code
Installing mingw/clang compiler for C & C++
Executing C++ program from command line
Edit and debug the program
Creating executable file
Running the file in command line
Useful links
Windows installation with instruction: https://code.visualstudio.com/docs/cpp/config-mingw
Windows instruction video: https://www.youtube.com/watch?v=4ridGE4jWi0
Mac installation with instruction: https://code.visualstudio.com/docs/cpp/config-clang-mac
Mac instruction video: https://www.youtube.com/watch?v=v2_Pth8MrKA
28
Example: Compound Interest Calculator
Given initial capital denoted as 𝐶0 , interest rate 𝑟, and a compounding period time 𝑡, the
interest is computed as 𝐼 = 𝐶0 𝑟𝑡
After 𝑖 fully finished compounding periods 𝐶𝑖 = 𝐶0𝑖 1 + 𝑟𝑡
If 𝑚 is the total number of months and 𝑟 is the equivalent annual interest rate for 12 months:
𝐶𝑖 = 𝐶0
𝑟
1+ 𝑡
12
𝑚
𝑡
29
Code Implementation
30
Code Implementation
31
Code Implementation
32
0
You can add this document to your study collection(s)
Sign in Available only to authorized usersYou can add this document to your saved list
Sign in Available only to authorized users(For complaints, use another form )