1 / 17

void main (void) { #ifdef UNCHANGEABLE_DEFINITION_AREA

Example Design Programming controls for an imaginary robot. The robot will have a tower. The tower will have 3 location sensors that are digital. The design will illustrate the use of user defined PWM signals. void main (void) { #ifdef UNCHANGEABLE_DEFINITION_AREA

osborn
Download Presentation

void main (void) { #ifdef UNCHANGEABLE_DEFINITION_AREA

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Example DesignProgramming controls for an imaginary robot.The robot will have a tower. The tower will have 3 location sensors that are digital.The design will illustrate the use of user defined PWM signals.

  2. void main (void) { #ifdef UNCHANGEABLE_DEFINITION_AREA IFI_Initialization (); /* DO NOT CHANGE! */ #endif User_Initialization(); /* You edit this in user_routines.c */ statusflag.NEW_SPI_DATA = 0; /* DO NOT CHANGE! */ while (1) /* This loop will repeat indefinitely. */ { #ifdef _SIMULATOR statusflag.NEW_SPI_DATA = 1; #endif if (statusflag.NEW_SPI_DATA) /* 26.2ms loop area */ { /* I'm slow! I only execute every 26.2ms because */ /* that's how fast the Master uP gives me data. */ Process_Data_From_Master_uP(); /* You edit this in user_routines.c */ if (autonomous_mode) /* DO NOT CHANGE! */ { User_Autonomous_Code(); /* You edit this in user_routines_fast.c */ } } Process_Data_From_Local_IO(); /* You edit this in user_routines_fast.c */ /* I'm fast! I execute during every loop.*/ } /* while (1) */ } /* END of Main */

  3. void User_Initialization (void) { //…Code Hidden CCP2CON = 0x3C; PR2 = 0xF9; CCPR2L = 0x7F; T2CON = 0; T2CONbits.TMR2ON = 1; Setup_PWM_Output_Type(USER_CCP,IFI_PWM,IFI_PWM,IFI_PWM); myInitCode(); //…CodeHidden }

  4. #include "cbt_controlMap.h" #include "cbt_robotComponents.h" Tower tower; void myInitCode() { TowerInit(&tower); }

  5. #ifndef __cbt_robotComponents_h_ #define __cbt_robotComponents_h_ #include "ifi_aliases.h" #include "ifi_default.h" #include "ifi_utilities.h" #define TOWER_TOP_POS 2 #define TOWER_MID_POS 1 #define TOWER_HOME_POS 0 #define TOWER_UNKN_POS -1 typedef struct { int destination; int position; } Tower; typedef Tower *TowerPtr; void TowerInit(TowerPtr tp); #endif

  6. #ifndef __cbt_controlMap_h_ #define __cbt_controlMap_h_ #define TOWER_TOP_SW p3_sw_top #define TOWER_MID_SW p3_sw_trig #define TOWER_HOME_SW p3_sw_aux1 #define TOWER_TOP_SENSOR rc_dig_in03 #define TOWER_MID_SENSOR rc_dig_in04 #define TOWER_HOME_SENSOR rc_dig_in05 #define TOWER_MOTOR CCPR2L #endif

  7. #include "cbt_robotComponents.h" void TowerInit(TowerPtr tp) { tp->destination = TOWER_HOME_POS; tp->position = TOWER_UNKN_POS; }

  8. void Process_Data_From_Master_uP(void) { Getdata(&rxdata); Default_Routine(); Generate_Pwms(pwm13,pwm14,pwm15,pwm16); Putdata(&txdata); }

  9. void Default_Routine(void) { driveControl(); towerControl(); }

  10. void driveControl(void) { pwm15 = p1_y; pwm16 = p2_y; }

  11. void towerControl(void) { if (TOWER_TOP_SW == 1) { tower.destination = TOWER_TOP_POS; } if (TOWER_MID_SW == 1) { tower.destination = TOWER_MID_POS; } if (TOWER_HOME_SW == 1) { tower.destination = TOWER_HOME_POS; } }

  12. void Process_Data_From_Local_IO(void) { calculateTowerPos(); moveTower(); }

  13. void calculateTowerPos(void) { int lightPos = pollPosSw(); if (tower.position == TOWER_UNKN_POS) { tower.position = lightPos; } else { if (lightPos != TOWER_UNKN_POS) { tower.position = lightPos; } } }

  14. int pollPosSw() { if (TOWER_HOME_SENSOR == 1) { return TOWER_HOME_POS; } if (TOWER_MID_SENSOR == 1) { return TOWER_MID_POS; } if (TOWER_TOP_SENSOR == 1) { return TOWER_TOP_POS; } return TOWER_UNKN_POS; }

  15. void moveTower(void) { if (tower.position != TOWER_UNKN_POS) { switch (tower.destination) { case TOWER_HOME_POS: if (tower.position != TOWER_HOME_POS) { TOWER_MOTOR = 0; } else { TOWER_MOTOR = 127; } break; case TOWER_MID_POS: if (tower.position == TOWER_HOME_POS) { TOWER_MOTOR = 255; } else { if (tower.position == TOWER_TOP_POS) { TOWER_MOTOR = 0; } else { TOWER_MOTOR = 127; } } break; case TOWER_TOP_POS: if (tower.position != TOWER_TOP_POS) { TOWER_MOTOR = 255; } else { TOWER_MOTOR = 127; } break; default: tower.destination = TOWER_HOME_POS; break; } } else { TOWER_MOTOR = 0; } }

  16. Encoder Counter #include "cbt_sensors.h“ void startCounting() { T1CONbits.TMR1ON = 1; } void stopCounting() { T1CONbits.TMR1ON = 0; } void resetCounter() { TMR1L = 0; TMR1H = 0; } int bump(CountingEncoderPtr_T1 ce, int n) { int count = TMR1H * 256 + TMR1L; ce->totalCount += count - ce->totalCount; ce->runningCount++; return 1; } void CountingEncoderInit_T1(CountingEncoderPtr_T1 cep) { cep->startCounting = startCounting; cep->stopCounting = stopCounting; cep->resetCounter = resetCounter; T1CON = 0b10000010; cep->resetCounter(); cep->totalCount = 0; cep->runningCount = 0; }

  17. Encoder Counter #ifndef __cbt_sensors_h_ #define __cbt_sensors_h_ #include "ifi_aliases.h" #include "ifi_default.h" #include "ifi_utilities.h" struct _countingEncoder; typedef struct _countingEncoder *CountingEncoderPtr_T1; void startCounting(); void stopCounting(); void resetCounter(); int bump(CountingEncoderPtr_T1 ce, int n); void CountingEncoderInit_T1(CountingEncoderPtr_T1 cep); typedef struct _countingEncoder { int totalCount; int runningCount; int (*bump)(int); void (*startCounting)(void); void (*stopCounting)(void); void (*resetCounter)(void); } CountingEncoder_T1; #endif

More Related