Intro to Mechatronics
Intro to Mechatronics ME 6405
Popular in Course
Popular in Mechanical Engineering
This 0 page Class Notes was uploaded by Chloe Reilly on Monday November 2, 2015. The Class Notes belongs to ME 6405 at Georgia Institute of Technology - Main Campus taught by Ifeanyi Ume in Fall. Since its upload, it has received 12 views. For similar materials see /class/234231/me-6405-georgia-institute-of-technology-main-campus in Mechanical Engineering at Georgia Institute of Technology - Main Campus.
Reviews for Intro to Mechatronics
Report this Material
What is Karma?
Karma is the currency of StudySoup.
You can buy or earn more Karma at anytime and redeem it for class notes, study guides, flashcards, and more!
Date Created: 11/02/15
Brief introduction to C by Akio Kita 1999 Note The objective of this guide is to introduce students to a simpli ed version of C Information from this guide will allow students to hack together a program with no regard to elegance style and optimization This hack mentality is demanded by the short period of time available to introduce this topic Pointers dynamic memory allocation operator overloading function overloading structures unions and othersubjects in C will not be discussed For a rigorous treatment of this subject please read the following The CProgramming Language by Brian W Kernighan DennisM Ritchie This book is written by the developers of C and explains the structure of the C language Practical C Pro rammin Nutshell Hanc mok by Steve Oualline Andy Oram This is a very good reference book to have available when programming Zen of Graphics Programming byMichael Abrash This book is focused on graphics programming but has many mathematical optimizations History In the early days of computers the only programming languages available to programmers were two low level languages machine and assembly language Programming in these languages was tedious and timeconsuming because the programmer had manage the hardware itself memory stack interrupts etc When computers became increasingly available many high level languages such as Fortran Pascal and Basic developed in order to make the underlying hardware invisible to the programmer and to make syntax easier therefore making computers easier and faster to program In 1972 the Unix Operating system was being developed During this time the concept of a system programming language having attributes of both low level and high level languages also developed System programming languages have advantages of a high level language but allow the programmer to take control of the underlying hardware if desired Brian W Kemigahn and Dennis M Ritchie developed C at Bell Laboratories as a system programming language Their underlying goal was to develop a language that was simple and exible enough to be used in a variety of different processors Two early versions were named A and B In 1983 the American National Standards Institute formed a committee to produce a C programming language standard This quotANSI Cquot was completed in 1988 Throughout its history C has been closely associated with the UNIX operating system with system programming in general and with the challenge of writing quotportablequot code that can be easily transferred to any one of many target computers In recent years several variants of C have developed C C with objects is the most popular programming language in the world and is the dominant in Microsoft Windows application development Small C is widely used for microcontrollers Java is used for distributed computing environments like the Intemet This discussion will be organized into two parts Part 1 will describe the building blocks of the C language operators separators variables functions if else statements and loops Part 2 will show the student how to create an actual C program Topics covered in part 2 include file structure preprocessor variable scope and library functions I ote C is case sensitive Part 1 1 Operators Operators are used to manipulate data Operators perform calculations check for equality make assignments and manipulate variables Of the 40 commonly used operators only 18 are essential for this discussion Assignment Operators Assignment X y Mathematical Operators Addition X X y Subtraction X X 7 y Multiplication X Xy Division X Xy Modulus X X y Logical Operators ampamp Logical AND X true ampamp false H Logical OR X true H false amp Bitwise AND X X amp OXFF l Bitwise OR X X l OXFF N Bitwise NOT X X N OXFF Logical NOT false true gtgt Shift Bits Right X X gtgt 1 ltlt Shift Bits Left X X ltlt 2 Equality Operators Equalto ifX 10 NotEqualto ifX 10 lt Lessthan ifXlt 10 gt Greater than if X gt 10 lt Less than or equal to if X lt 10 gt Greater than or equal to if X gt 10 Note Mathematical Operators follow standard precedence Multiplication and divisionwill be eXecuted before any addition or subtraction Separators There are 5 types of separators needed for this discussion These separators are used to group together lines of code and initialize arrays This separator is used to separate lines of code These separators are used to specify parameters and to change mathematical operator precedence Example Changing mathematical operator precedence 5 7 2 l9 5 7 2 24 These separators are used to index arrays These separators indicate a string These separators indicate a comment 2 Data Types In C a data type de nes the way the program stores information in memory The size in bytes a data type uses varies between every development system so be careful Data T e Descri tion Size in B tes most common void nothing 0 char character 1 int integer 2 long integer 4 oat oating point 4 double oating point 8 All ofthese data types are signed The most significant bit is used to indicate positive or negative A type modifier unsigned can be used to use the most significant bit Example int possible range of values 32768 to 32767 unsigned int possible range of values 0 to 65535 Every variable used in a program must be declared Example int myvariable You can assign an initial value to an array by using the assignment operator Example int myvariable 5 An array is a collection of values An array is allocated using the separator Example Declaring an array of integers that holds ve values int myarray5 If you want to assign an initial value to an array you would use the separators Example Filling an array with an initial value int myarray5 300 400 2 4 5 If initial values of the array are given the compiler is smalt enough to gure out how many positions are needed to hold the initial values Example Alternative way to declare an array with initial values int myarray 300 400 2 4 5 A string is an array of character values Example Declaring a string char mystring 10 To initialize a string the assignment operator and separators are used The compiler will also place a NULL character NULL 0 at the end of the string to indicate the end of a string Example Initializing a string char mystring Hello To use a particular value or character in an array you can index the value using the separators ote Index of an array starts at 0 Example Indexing myarray2 from above is equal to 72 mystringl from above is equal to e 3 Functions Functions are the building blocks of a C program Functions are sections of code that perform a single welldefined service All executable code in C is within a function Functions take any number of parameters and return a maximum of 1 value A parameter is a value passed to a function that is used to alter its operation or indicate the extent of its operations A function in C looks like this Return Type Function Name Parameter list int Multiplyint x int y int 239 Function Body gt Z X y return 239 T Return Statement In the data type section you might have wondered what the void type was used for If a function does not return a value or there are no parameters the type void is used Example void PrintHellovoid printf Hello World printf prints a string to the screen return Another question you might ask is which function is executed rst in a program In C the function named main is executed first Like variables all function except the main function must be declared before it is defined Example Declaring a function void PrintHellovoid Note An array as a parameter has this form in the declaration void A1rayFunctionint SomeArray Then when calling the function give the function the array s name int MyArraylO A1rayFunctionMyArray 4 If and else statements The if statement is a conditional statement If the condition given to the if statement is true then a section of code is executed Syntax ifcondition execute this code if condition is true Example int absoluteint x ifx lt 0 x1x return x Sometimes you want to perform an action when the condition is ture and perform a different action when the condition is false Then you can use the else statement Syntax ifcondition execute this code if condition is true else execute this code if condition is false It is very easy to chain a lot of if else statements Syntax ifcondition I execute this code ifcondition 1 is true else ifcondition 2 execute this code ifcondition 1 is false and condition 2 is true else execute this code ifcondition 1 and condition 2 are false 5 Loops It has been proven that the only loop needed for programming is the while loop While the condition given to the while loop is true a section of code is executed Syntax whilecondition execute this code while the condition is true Example unsigned int factorialunsigned int X unsigned int result whileX gt 0 result resultX X X 71 return result You can break out of a while loop prematurely by using the break statement Example int X 0 whiletrue ifX 10000 break XXl In this eXample the condition given to the while loop is always true Once X equals 10000 the program will eXit the while loop The other major loop is the for loop It takes three parameters the starting number the test condition that determines when the loop stops and the increment expression Syntax for initial condition adjust code to be executed while the condition is true The for loop repeatedly executes a section of code while the condition is true The statement initial initializes the state of the loop After the section of code is executed the state is modi ed using the statement adjust Example unsigned int factorial unsigned x int counter unsigned int result l for counter 1 counter lt x counter counter 1 result resultx return result End Part 1 Part 2 1 File Types Every C compiler adheres to certain rules regarding le extensions Extension lib These are C source files They usually contain only functions and preprocessor commands with one exception Any C source le containing the function main also includes global variable declarations for variables used in the le and function declarations for functions defined in the le Every C source le without the main function has a header le with the same name The header le includes preprocessor commands global variable declarations for variables used in the associated C source le and function declarations for functions de ned in the associated C source le After the compiler compiles the C source le a object le with the same name is created This is done primarily to save the compiler time The compiler will only compile les that have recently been changed If the source le will never change then the programmer can create a library le The programmer can then use the functions in the library le The main function is not allowed in a library le This is the only le extension that varies from system to system When a user compiles a program a compiler creates object les from the source les then a linker links any functions used in the object les together to create an executable le 2 Library Functions C development packages always contain standard libraries In essence standard functions are provided to the programmer The standard libraries we will be using include stdi0lib Standard Input and Output int printfc0nst char format argument Accepts a series of arguments Applies to each argument a format speci er contained in the format string format Outputs the formatted data to the screen To use this function we have to give it the name of a character array or a string itself Example Printing a string array char myString Hello World printfmyString Example Printing with a actual string printf Hello World Using arguments and format speci ers we can print a variable s value to the screen Format specifiers can vary between development systems Format Specifiers d i o u x X f c s Integer Integer Integer Integer Integer Integer Floating point Character String pointer None signed decimal integer signed decimal integer unsigned octal integer unsigned decimal integer unsigned hexadecimal int with a b c d e f unsigned hexadecimal int with A B C D E F signed value Single character Prints characters until a nullterminator Prints the character Example Printing a variable s value int x 5 int y xx printf d squared equals d xy The above example will print 5 squared equals 25 onto the screen To display non graphical characters such as a newline character and separators an escape sequence is needed n Escape Seguences LF CR HT Newline linefeed Carriage return Tab horizontal Backslash Single quote apostrophe Double quote Question mark int scanfc0nst char format address 0 scans a series of input elds one character at a time 0 formats each eld according to a corresponding format speci er passed in the format string Scanf is similar to printf It is used to obtain user input There must be one at least one address argument To get an address argument use the amp operator in front of variable names but not in front of character arrays the amp operator is an address of operator Example Entering a number int x printf Please input an integer scanf d ampx Example Entering a string int char buffer80 printf Please input a string scanf s buffer mathlib Math Libraries There are many functions in the math library to do standard mathematical operations such as sin cos tangent exponent log etc You can look at the associated header file mathh to nd out the parameter list and return type for each function There are also nonmathematical functions in the math library These functions are used to convert a string into an integer or oating point value These are not required for this discussion 3 PreProcessor PreProcessor directives are instructions for the compiler Preprocessor directives are pre xed by the character For this discussion only 2 different preprocessor commands are needed include lt lenamehgt The include directive is used to link C source les object les and library les together For this discussion we will use this to link the library les This directive must appear before global variable declarations and function declarations de ne NAME VALUE The de ne directive is used to set de nitions Example de ne PI 314 4 Return to Variables and Functions Scope We should say a little more about the mechanism by which an argument is passed to a function Formally C is call by value which means that a function receives copies of the values of its parameters Suppose we create a function that multiplies a number by 2 int multwoint X X X 2 return X We might wonder if we wrote it this way what would happen to the value of the variable 2 if we write this code 2 5 j multtwoz When our implementation of multtwo changes the value of X does that change the value of 2 when we use it as a parameter The answer is no X receives a copy of 239s value so when we change X we don t change 2 There is an eXception to this rule When the argument passed to a function is an array the function does not receive a copy of the array and it therefore can modify the arraySo how do we change multiple values with a single function We can put all of our variables in array form but that is not a very elegant solution The elegant way to solve this problem is to use pointers but since we will not be using pointers in our discussion we turn to variable scope Before discussing variable scope lets draw a diagram to help eXplain it Levels Base Level Level 1 a inside a function Level 2a Level 2b Level 1 b inside afunciion It is clear that levels are separated by the separator Any variable declared in a level is only available to higher numbered levels If a variable is declared in Level la only Level 2a and Level 2b will have access to the variable If a variable is declared in Level 2a Level 2b will not have access to that variable because it is not a higher numbered level If we declare a variable in the Base Level then it becomes a global variable Any functions de ned in the same le will be able to use that variable This is how we get functions to change multiple variables Note Good programmers frown upon global variables but since we cannot use pointers we have no choice If you use a global variable and use it in a function that function loses its portability 5 Putting it all together With all of the information you gained above and perusing some library header les you should be able to put together a functional C program To illustrate here is a program to output a integer to binary Example Read the comments for explanations Example Integer to binary conversion pre processor commands come first include ltstdiohgt global variables and function declarations come neXt int IntToBinaryint X int bin void mainvoid int X Declare an integer variable int limit Declare an integer variable int binaryarray20 Declare an integer array Let the user input a variable and make sure it is positive whiletrue printf n Input a positive integer scanf d ampx ifx lt m printf n Not a positive integer else break
Are you sure you want to buy this material for
You're already Subscribed!
Looks like you've already subscribed to StudySoup, you won't need to purchase another subscription to get this material. To access this material simply click 'View Full Document'