Wednesday, May 27, 2015

Functions and Stack



                       ###########                                                           ######## 
                       # Registers #                                                           #  Stack #
                       ###########                                                           ########
###################################        ####################################
####            Stack Pointer (sp)                ###       ###                local variable n                    ###
###################################        ####################################
####              Fame Counter (fc)             ###       ###                .........................                    ###
###################################        ####################################
####            Link Register (lr)                ###       ###                 local variable 1                  ###
###################################        ####################################
####           Program Counter (pc)          ###       ###             Passed Parameter n                ###
###################################        ####################################
                                                                              ###             ...............................                ###
                                                                              ####################################
                                                                              ###             Passed Parameter 1                ###
                                                                              ####################################
                                                                              ###    caller function lr (return address)   ###
                                                                              ####################################
                                                                              ###  caller function fp (frame pointer)     ###
                                                                              ####################################
                                                                              ###              same block repeats              ###
                                                                              ###              for caller functions              ###
                                                                              ###              ................................              ###
  • Stack pointer (sp) register:
    • Is a HW register, which points to the top of the stack
    • As local variables are defined, they are added to the stack, and the stack pointer value changes.
    • Since the stack pointer is changing, we can not reference the function parameters and local variables as offset from the sp value
    • Instead, we reference the function parameters and variables by adding offset to the frame pointer (fp)
  • Frame Pointer (fp) register
    • Frame pointer register, points to the bottom of the stack part that is related to the current function
    • By other words, fp = sp, before adding the function parameters or local variables.
    • fp value is constant for a function, hence it is used to reference the function parameters and local variable.
  • Program Counter (pc) register:
    • A register that points to the next instruction to be executed.
  • Link Register (lr):
    • When calling a function, a branch happens from the normal sequence to the address of this function.
    • The return address is saved in the link register.
    • After the function finishes, pc = lr
  • When a function call happens:
    • Current lr is saved to stack --> Update sp
    • lr = pc + 1 (return address is saved to lr)
    • Current fp is saved to stack --> Update sp
    • Current sp is saved to fp
    • Parameters are moved to stack --> Update sp
    • Local variables are created on stack --> Update sp
    • Parameters and local variables are refered to as [fp + offset]
    • After the function is finished:
      • It returns the return value in a special register
      • sp = fp
      • pc = lr
      • fp = saved fp
      • lr = saved lr



Sunday, May 24, 2015

Embedded C Build Process


  • Pre-Processor:
    • .c + .h --> .i
    • .c + .h --> .i
    • .c + .h --> .i
    • Stripes out the comments
    • Substitute the #include
    • Substitute the # macros
  • Compiler:
    • .i --> .s --> .o
    • .i --> .s --> .o
    • .i --> .s --> .o
    • from c to assembly
    • the assembly is written in machine code
    • each .o file contains:
      • symbol table
        • symbol name | address in memory | symbol size | symbol section (bss - data - rodata - text - ...etc)
        • all addresses are relative to the file
        • for external variables, the addresses are not determined. (to be determined after linking)
      • sections
        • bss: uninitialized global variables
        • data: initialized global variables
        • rodata: constants
        • text: code
  • Linker:
    • .o + .o + .o + startup.o + standard_libs.o --> app.o
    • standard_libs.o
      • to resolve functions like printf(), ...etc
    • startup.o
      • disable all interrupts
      • copy initialized variables from ROM to RAM
      • initialize uninitialized data
      • allocate space for stack and initialize it
      • initializes the processor stack pointer
      • create and initialize the heap
      • enable interrupts
      • call main
      • startup is written by the developer as assembly or c, then compiled and linked with the application
      • When the reset line is disabled (once the processor start working), the program counter points to the start up
      • "Start up" can be called "boot loader", usually it is called a boot loader if it comes more complicated than just doing the above points.
        A "boot loader" can load OS, initialize HW, ...etc.
        If a boorloader is interupted "by a push button, or an escape method, ...etc", it stops loading the main program, and wait for a new main program to be flashed ...etc.
    • app.o:
      • all text, bss, data, rodata sections of all the input object files are merged together, hence:
        • symbol table contains zero unresolved symbols
        • bss contains all uninitialized variables
        • data contains all initialized variables
        • rodata contains all constants
        • text contains all code
      • In case of embedded systems: 
        • the addresses needs to be mapped to the system memory
        • this is done using a "linker script" in the "locating" process
  • Locator:
    • app.o + linker script --> targetFile
    • To be continued



You can use the files in the this link, a long with the following commands to see the above info in action:
// to run preprocessor-compiler-linker-locator, saving all temp file
gcc main.c add.c -o adder -save-temps
// you can view the preprocessor output
*.i
// you can view the assembly
*.s
// to view the object files, and target file:
objdump -d -t -s adder > adderObjectDump
objdump -d -t -s main.o > mainObjectDump
objdump -d -t -s add.o > addObjectDump

    where:  -d // display the executable sections in assembly form (disassemble)
                 -t // print symbol table
                 -s // display the contents of the sections

Tuesday, April 28, 2015

Convert rmvb to avi

To convert rmvb to avi, you may try:


#!/bin/bash
cd /home/username/films
for file in *.rmvb; do
  mencoder "$file" -oac mp3lame -ovc lavc -o "/home/username/films/avifilms/${file%.rmvb}.a$
done

where, the videos you want to convert is in "/home/username/films"

LifeHacker - find/burn subtitle

To find and burn subtitles to a movie, you may try lifehacker

please follow the link:
http://lifehacker.com/how-to-get-subtitles-on-your-digital-movies-489535336

Sunday, April 26, 2015

الرومان

من كتاب قصة الحضارة:ـ

أ- الجمهورية
ب - الثورة
ج - الملكية


في عهد الملكية - تدين أقل - انتشار الزنا و قلة الزواج - في الطبقات العليا: استخدام موانع الحمل الآلية و الكيميائية - في الطبقات العليا:  استخدام عمليات الإجهاض - الطبقات الفقيرة: زيادة النسل

الثياب

tunic
جميع الطبقات تلبسه

 

Toga جبة

يلبسه الأشراف في مجلس الشيوخ أو الألعاب

 


أما الامبراطور فيلبس طوجة أرجوانية


الخف البسيط، و هو شبه الشبشب بصباع مصنوع من الجلد
الحذاء الكامل من القماش والجلد، و كان يرتدى مع الطوجة في المناسبات


أما النساء فبعضهن كانوا يلبسون الخمار، والبراقع لتخفي الوجه، وبعضهن كانوا يزينون شعورهم




كانت الألعاب تقام في الاحتفالات بالأعياد الدينية أو المناسبات الخاصة بالامبراطور، وكان المجالدين من
 المسجونين و المحكوم عليهم بالإعدام يصارعون بعضهم في مبارزات فردية أو جماعية، ويتسلحون بالسهام أو الحراب أو الخناجر، وقد يلبسون الدروع، وقد يحاربون الحيوانات

كان المجتلد ذو أرض خشبية مغطاه بالتراب، فوقها شرفة  للشيوخ و الكهنة و كبار الموظفين، فوقهم مقصورة للامبراطور و الامبراطورة والأسرة الامبراطورية والحاشية، وفوقهم الأشراف، وكان يسمح لكل الأحرار بالمشاهدة

بعد فازباسيان صارت المجالدات تقام في الكولسيوم بدلا من المجتلد

18 july 64
 شب حريق في المضمار الكبير، وانتقل إلى  ثلثي المدينة لمدة 9 أيام
كان نيرون يريد أن يعيد بناء المدينة بشكل أكثر تنظيما، أشيع أنه متواطئ في حرق المدينة، لما وجد ذلك، اتهم المسيحيين بحرقها لأنهم كانوا يبشرون بزوال هذه الحياة الظالمة و استبدالها بحياة أفضل

أصدر على عدد من المسيحيين أحكام بالإدانة من غير دليل على حرقهم رومة، بل كانت التهمة هي كراهية الجنس البشري، ألبس بعضهم الجلود و تركهم للحيوانات تأكلهم، وصلب البعض، ودفن الكثير 

منهم أحياء، وأشعل في بعضهم النيران

صلب بطرس بالمقلوب في ملعب نيرون عام 64 في حلبة روما في ميدان الفاتيكان 

من كتاب مختصر تاريخ العالم :ـ
 " شرع نيرون يعتقل المسيحيين حيث يجدهم، ثم كانوا يعدمون بوحشية" وكانوام  يدفنون موتاهم في شبكة من الممرات تحت الأرض خارج أسوار المدينة، و أيضا  يتجمعون في هذه الممرات ليشجعوا بعضهم في أوقات الاضطهاد

Monday, April 13, 2015

Linear System Model

A) Linear system with one state variable, one input and one output

Any Linear system can be described through linear model as below:

  • Specify the state
    • ex: position (x)
  • Specify the inputs
    • ex: velocity (v)
  • Specify the output
    • ex: position (x)

You will have your system described as follows:
  • Differential equation







  • Output equation
y = x

B) Linear system with multiple state variable, one input and one output


Suppose that you have a linear system with:
  • State: x, y, PHI
    • i.e: x1 = x, x2=y, x3=Φ
  • Inputs: nu, PHI
    • i.e: u1 = nu, u2 = Φ
  • Output: x,y,PHI
    • i.e: y1 = x, y2=y, y3=Φ
You will have your system described as follows:





  • Differential equations


  • Output equations

The above equations can be written using Matrices as follows:

  • Differential equations


  • Output equations













in our example, C11, C22, C33 are equal to one, others are zeros


C) Nonlinear systems (Linearization)

If we have systems that are described by nonlinear differential equations, where you can find terms like:
  • x1*x2
  • x1*u1
  • cos(x3)
  • ...etc
We can linearize them around a certain point of operation (x0,u0)

  • Equations will be in the form:


We can linearize this around x0, u0 (specific state and specific input)







































Note, the above equations assumes that x0, u0 satisfies: f(x0,u0) = 0, h(x0) = 0






























Friday, April 3, 2015

Mobile Robots

Behaviors:

Mobile Robots Models:

(a) Differential Wheel Drive Model     --> Used for implementation (real model)
(b) Unicycle Model                               --> Used for design (more simple)

The following snapshot, is taken from the Glue Lecture of Lecture 2 of the Coursera course: Control Of Mobile Robots

where,

  • νr, νl: the angular velocities of the right and left wheel
  • c: the velocity of the robot
  • R: the radius of each wheel
  • Ф: the angle between the x axis and the robot direction
  • ω: the angular velocity of the robot (rate of change of Ф)
So, what we do is:
  • Use the Unicycle model for design (get: ν,ω)
  • Convert to the Differential Wheel Drive model from implementation (solve above equations -> get νr, νl from ν,ω )


Wheel Encoders:

They are wheel ticks counter, where the wheel has N ticks per revolution.
The wheel encoder counts the number of wheel ticks elapsed during rolling.

Knowing previous x,y,Ф and the readings of the wheel encoders, we can get the current x,y,Ф as follows:
The following snapshot, is taken from the Glue Lecture of Lecture 2 of the Coursera course: Control Of Mobile Robots



But, if the robot slipped, for example because it hit another robot, then its movement will not be related to the wheel encoder reading!

in this case, we shall use sensors (IR - Ultrasonic - LaserScanner - Camera - ...etc)

Behavior:

The robot may have one or more controllers (behaviors):

  • Go-To-Goal
  • Avoid-Obsatcles
  • Track-Target
  • ...etc
Mobile Robot Control Block Diagram:


The following snapshot, is taken from the Glue Lecture of Lecture 2 of the Coursera course: Control Of Mobile Robots