erlerobot / smart_motor

4 stars 2 forks source link

Identify and comment the code of OpenServo #9

Open jlamperez opened 8 years ago

jlamperez commented 8 years ago

Identify and comment the code of OpenServo

Software Components

The OpenServo consists of three main software components.

Embedded bootloader When the OpenServo is powered on, the first thing that is executed is the bootloader. The bootloader allows for new application code to uploaded within 3 seconds of power on. The bootloader initialises at the standard address of 0x7F by default. This behaviour can be altered.

Embedded Application code Once the bootloader has timed out, the application code is booted. The application code is the main controller of the servo hardware. The main application code is uploaded to the servo from the bootloader, facilitating simple updates of the OpenServo application firmware.

Host Control software Once the OpenServo is booted and running, you can communicate with it using the TWI (I2C) interface. You will need a host controller to communicate with the OpenServo, whether that is a PC to I2C bridge, or another MCU.

Link


There are two repositories for the OpenServo proyect:

The folders here are:

Inside this folder there are different versions of OpenServo:

AVR_OpenServo_V3.

Used code for the first OpenServo prototype. It has less registers than the -dev branch.

The first 5 files in this folder are created after compile the .c and .h files below:

After we have the Makefile and the README.txt.

The code is organized in modules where each has a functionality:

  1. adc #12
  2. curve #15
  3. eeprom #14
  4. enc
  5. motion #19
  6. pid #13
  7. power #16
  8. pulsectl
  9. pwm #30
  10. registers #18
  11. seek(obsolete)
  12. swi2c
  13. timer #20
  14. twi #22
  15. watchdog #17 .

To finish the analize the code we have this special files:

  1. config.h #21-> File where differents options for the servo can be set.
  2. Openservo.h -> File where some parameters like device type and subtype, software version and defalut TWI address i specified.
  3. main.c -> Where programme is executed. Here the different modules are initialized and where the main processing loop for the servo is. It basically looks for new position, power or TWI commands to be processed.
  4. bootcrt.s -> ???
  5. macros.h -> ???

    AVR_OpenServo_V3-dev.

Code under development. More interesting features.

The first 5 files in this folder are created after compile the .c and .h files below:

There is a config folder where some files with parameters are defined:

  1. motor_hardware.h
  2. pindefs_OSv2.h
  3. pindefs_OSv3.h
  4. pindefs_RobotFuzz_OpenStep.h
  5. pindefs_example.h
  6. servo_hardware.h
  7. target_platform.h

The code is organized in modules where each has a functionality:

  1. adc #12 1_1. alert 1_2. backemf 1_3. banks
  2. curve #15
  3. eeprom #14
  4. enc <---------- not in dev 4_1. filter 4_2. heartbeat
  5. motion #19
  6. pid #13
  7. power #16
  8. pulsectl 8_1 pwm-std
  9. pwm #30
  10. registers #18
  11. seek(obsolete) 11_1 step
  12. swi2c <--------------- not in dev
  13. timer #20
  14. twi #22
  15. watchdog #17 .

To finish the analize the code we have this special files:

  1. config.h #21-> File where differents options for the servo can be set.
  2. Openservo.h -> File where some parameters like device type and subtype, software version and defalut TWI address i specified.
  3. main.c -> Where programme is executed. Here the different modules are initialized and where the main processing loop for the servo is. It basically looks for new position, power or TWI commands to be processed.
  4. macros.h -> ??? Not bootcrt.s

    AVR_OpenServo_v3_experimental

[TODO]

AVR_OpenStepper

folder which allow for a standard 4 pole stepper motor to be driven from an AVR attiny84 [TODO]

AVR_VelocityControl

the algorithm compares the seek velocity with the actual velocity measured with the back-emf method and generates an apropriate motor PWM (PI+FF algorithm) [TODO]

OpenServo v4

The code as V3-dev has a config folder where this files appeared:

  1. motor_hardware.h
  2. pindefs_OSv2.h
  3. pindefs_OSv3.h
  4. pindefs_OSv4.h <-- Not in V3-dev
  5. pindefs_RobotFuzz_OpenStep.h
  6. pindefs_example.h
  7. servo_hardware.h
  8. target_platform.h

Then there is a folder called include with this .h files:

  1. OpenServoCommon.h -> Device type, subtype, version major, minor and default twi address defined.
  2. banks.h -> the accesible register ordered in banks. Important banks 0 and 1.
  3. config.h -> This file contains overridable defaults for the OpenServo hardware. This also serves to load up the config files of the hardware available in the config/ directory.
  4. getter.h -> This is a class masquerading as a "library" in the standard Wiring fashion.
  5. i2c_module.h -> include "libmaple/i2c.h". Three functions: void i2c_module_init();void funcrx(i2c_msg msg); void functx(i2c_msg msg);
  6. os_adc.h -> Current, Position, BackEMF and Temperature extern declarations.
  7. packetcommands.h -> TWI commands.(0x80 - 0x98)
  8. pid.h -> pid interface.
  9. pwm.h -> pwm interface.
  10. pwm_os.h -> Same as above.
  11. randomizer.h -> A useless front-end to the standard C random number interface. This is basically the same thing as the wirish functions randomSeed() and random().
  12. registers.h -> registers of OpenServo.
  13. setup.h

The we have the impelementation of the functions in cpp:

  1. FreeRTOSConfig.h -> Configuration file of FreeRTOS.
  2. adc.cpp -> Implementation of os_adc.h functions.
  3. banks.cpp -> Implementation of Banks.h. Contains WProgram.h
  4. i2c_module.cpp -> I2c_module implementation
  5. main.cpp -> See real-main,cpp
  6. make-helper.sh
  7. packetprocess.cpp -> For TWI command implementation. Uses WProgram.h
  8. pid.cpp -> PID implementation
  9. pwm.cpp -> PWM implementation. Uses WProgram.h to encapsulate.
  10. real-main.cpp -> Main. Uses a scheduler. Also uses FreeRTOSCongi.h file.
  11. registers.cpp -> Registers implementation.
  12. rules.mk -> Makefile
  13. setup.cpp -> Configure the default pin configuration on startup of the device, setup modules and setup OpeServo.

    Differences between code:

    V3 and V3-dev

    • In V3 there is bootcrt.s file that is not in V3-dev
    • In V3- dev there is a config folder and this modules are added 1_1. alert, 1_2. backemf, 1_3. banks, 4_1. filter, 4_2. heartbeat, 8_1 pwm-std, 11_1 step.
    • In V3-dev enc and swi2c modules used to connect an encoder are deleted.

      V3-dev and V4.

    • Main differences: V4 is written in .cpp. Also uses FreeRTOS, to create task and schedule them and WProgram.h, that encapsulates code, for example pinMode() and digitalWrite() also used in Arduino.

      Analized Code

After compiling the code you have to be sure that the .hex files have been created. If not verify in the proyect properties if if the Generate Hex file for Flash memory and EEPROM are checked:

Changes in the code: Interruption names have changed and have new name: (pulsectl??)

SIGNAL(TIMER0_COMPA_vect)
// Handles timer/counter0 compare match A.
--------------------------
SIGNAL(ADC_vect)
// Handles ADC interrupt.
--------------------------
SIGNAL(TWI_vect)
// Handle the TWI interrupt condition.
--------------------------
SIGNAL(TIMER2_OVF_vect)
// Handles timer/counter2 overflow interrupt.
--------------------------
SIGNAL(PCINT0_vect)
// Handles pin change 0 interrupt.

Bootloader ATmega

See #25

jlamperez commented 8 years ago

Improvements on the code: