code petit robot pour homologation
Fork of CRAC-Strat_2017_V2 by
Instruction/Instruction.cpp@4:88431b537477, 2016-04-17 (annotated)
- Committer:
- antbig
- Date:
- Sun Apr 17 14:03:03 2016 +0000
- Revision:
- 4:88431b537477
- Parent:
- 1:116040d14164
- Child:
- 11:ed13a480ddca
Ajout possibilit? de choisir id strat via CAN
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
antbig | 0:ad97421fb1fb | 1 | #include "Instruction.h" |
antbig | 0:ad97421fb1fb | 2 | |
antbig | 0:ad97421fb1fb | 3 | |
antbig | 0:ad97421fb1fb | 4 | char cheminFileStart[SIZE+8]="/local/"; |
antbig | 0:ad97421fb1fb | 5 | struct S_Instruction strat_instructions[SIZE_BUFFER_FILE];//Liste des instructions |
antbig | 0:ad97421fb1fb | 6 | unsigned char nb_instructions = 0;//Le nombre d'instruction dans le fichier de strategie |
antbig | 0:ad97421fb1fb | 7 | unsigned char actual_instruction = 0;//La ligne de l'instruction en cours d'execution |
antbig | 0:ad97421fb1fb | 8 | |
antbig | 0:ad97421fb1fb | 9 | LocalFileSystem local("local"); |
antbig | 0:ad97421fb1fb | 10 | |
antbig | 0:ad97421fb1fb | 11 | enum E_InstructionType charToInstructionType(char type) |
antbig | 0:ad97421fb1fb | 12 | { |
antbig | 0:ad97421fb1fb | 13 | switch(type) |
antbig | 0:ad97421fb1fb | 14 | { |
antbig | 0:ad97421fb1fb | 15 | case 'C': return MV_COURBURE; |
antbig | 0:ad97421fb1fb | 16 | case 'L': return MV_LINE; |
antbig | 0:ad97421fb1fb | 17 | case 'T': return MV_TURN; |
antbig | 0:ad97421fb1fb | 18 | case 'X': return MV_XYT; |
antbig | 0:ad97421fb1fb | 19 | case 'R': return MV_RECALAGE; |
antbig | 0:ad97421fb1fb | 20 | case 'A': return ACTION; |
antbig | 0:ad97421fb1fb | 21 | default: return UNKNOWN; |
antbig | 0:ad97421fb1fb | 22 | } |
antbig | 0:ad97421fb1fb | 23 | } |
antbig | 0:ad97421fb1fb | 24 | |
antbig | 0:ad97421fb1fb | 25 | enum E_InstructionDirection charToInstructionDirection(char type) |
antbig | 0:ad97421fb1fb | 26 | { |
antbig | 0:ad97421fb1fb | 27 | switch(type) |
antbig | 0:ad97421fb1fb | 28 | { |
antbig | 0:ad97421fb1fb | 29 | case 'B': return BACKWARD; |
antbig | 0:ad97421fb1fb | 30 | case 'F': return FORWARD; |
antbig | 0:ad97421fb1fb | 31 | case 'R': return RELATIVE; |
antbig | 0:ad97421fb1fb | 32 | case 'A': return ABSOLUTE; |
antbig | 0:ad97421fb1fb | 33 | default: return NODIRECTION; |
antbig | 0:ad97421fb1fb | 34 | } |
antbig | 0:ad97421fb1fb | 35 | } |
antbig | 0:ad97421fb1fb | 36 | |
antbig | 0:ad97421fb1fb | 37 | enum E_InstructionPrecisionOuRecalage charToInstructionPrecisionOuRecalage(char type) |
antbig | 0:ad97421fb1fb | 38 | { |
antbig | 0:ad97421fb1fb | 39 | switch(type) |
antbig | 0:ad97421fb1fb | 40 | { |
antbig | 0:ad97421fb1fb | 41 | case 'P': return PRECISION; |
antbig | 0:ad97421fb1fb | 42 | case 'X': return RECALAGE_X; |
antbig | 0:ad97421fb1fb | 43 | case 'Y': return RECALAGE_Y; |
antbig | 0:ad97421fb1fb | 44 | default: return NOPRECISION; |
antbig | 0:ad97421fb1fb | 45 | } |
antbig | 0:ad97421fb1fb | 46 | } |
antbig | 0:ad97421fb1fb | 47 | |
antbig | 0:ad97421fb1fb | 48 | enum E_InstructionNextActionType charToInstructionNextActionType(char type) |
antbig | 0:ad97421fb1fb | 49 | { |
antbig | 0:ad97421fb1fb | 50 | switch(type) |
antbig | 0:ad97421fb1fb | 51 | { |
antbig | 0:ad97421fb1fb | 52 | case 'J': return JUMP; |
antbig | 0:ad97421fb1fb | 53 | case 'W': return WAIT; |
antbig | 0:ad97421fb1fb | 54 | case 'E': return ENCHAINEMENT; |
antbig | 0:ad97421fb1fb | 55 | default: return NONEXTACTION; |
antbig | 0:ad97421fb1fb | 56 | } |
antbig | 0:ad97421fb1fb | 57 | } |
antbig | 0:ad97421fb1fb | 58 | |
antbig | 0:ad97421fb1fb | 59 | enum E_InstructionNextActionJumpType charToInstructionNextActionJumpType(char type) |
antbig | 0:ad97421fb1fb | 60 | { |
antbig | 0:ad97421fb1fb | 61 | switch(type) |
antbig | 0:ad97421fb1fb | 62 | { |
antbig | 0:ad97421fb1fb | 63 | case 'T': return JUMP_TIME; |
antbig | 0:ad97421fb1fb | 64 | case 'P': return JUMP_POSITION; |
antbig | 0:ad97421fb1fb | 65 | default: return NONEXTACTIONJUMPTYPE; |
antbig | 0:ad97421fb1fb | 66 | } |
antbig | 0:ad97421fb1fb | 67 | } |
antbig | 0:ad97421fb1fb | 68 | |
antbig | 4:88431b537477 | 69 | /****************************************************************************************/ |
antbig | 4:88431b537477 | 70 | /* FUNCTION NAME: stringToInstruction */ |
antbig | 4:88431b537477 | 71 | /* DESCRIPTION : Conversion d'une ligne du fichier de strat en instruction */ |
antbig | 4:88431b537477 | 72 | /****************************************************************************************/ |
antbig | 0:ad97421fb1fb | 73 | struct S_Instruction stringToInstruction(char line[]) { |
antbig | 0:ad97421fb1fb | 74 | struct S_Instruction instruction; |
antbig | 0:ad97421fb1fb | 75 | |
antbig | 0:ad97421fb1fb | 76 | char instructionOrder; |
antbig | 0:ad97421fb1fb | 77 | char instructionDirection; |
antbig | 0:ad97421fb1fb | 78 | char instructionPrecision; |
antbig | 0:ad97421fb1fb | 79 | char instructionNextActionType; |
antbig | 0:ad97421fb1fb | 80 | char instructionJumpAction; |
antbig | 1:116040d14164 | 81 | int errorCode = 0; |
antbig | 0:ad97421fb1fb | 82 | /* |
antbig | 0:ad97421fb1fb | 83 | Info sur la fonction sscanf |
antbig | 0:ad97421fb1fb | 84 | %d -> Entier signé |
antbig | 0:ad97421fb1fb | 85 | %u -> Entié non signé |
antbig | 0:ad97421fb1fb | 86 | %c -> char |
antbig | 0:ad97421fb1fb | 87 | */ |
antbig | 1:116040d14164 | 88 | errorCode = sscanf(line, "%d,%c,%c,%u,%u,%d,%c,%c,%c,%u,%u,%d,%d", |
antbig | 0:ad97421fb1fb | 89 | &instruction.lineNumber, |
antbig | 0:ad97421fb1fb | 90 | &instructionOrder, |
antbig | 0:ad97421fb1fb | 91 | &instructionDirection, |
antbig | 0:ad97421fb1fb | 92 | &instruction.arg1, |
antbig | 0:ad97421fb1fb | 93 | &instruction.arg2, |
antbig | 0:ad97421fb1fb | 94 | &instruction.arg3, |
antbig | 0:ad97421fb1fb | 95 | &instructionPrecision, |
antbig | 0:ad97421fb1fb | 96 | &instructionNextActionType, |
antbig | 0:ad97421fb1fb | 97 | &instructionJumpAction, |
antbig | 0:ad97421fb1fb | 98 | &instruction.JumpTimeOrX, |
antbig | 0:ad97421fb1fb | 99 | &instruction.JumpY, |
antbig | 0:ad97421fb1fb | 100 | &instruction.nextLineOK, |
antbig | 0:ad97421fb1fb | 101 | &instruction.nextLineError |
antbig | 0:ad97421fb1fb | 102 | ); |
antbig | 1:116040d14164 | 103 | /* |
antbig | 1:116040d14164 | 104 | if(errorCode != 13) { |
antbig | 1:116040d14164 | 105 | errorInstructionLoop();//L'instruction est pas bonne !! |
antbig | 1:116040d14164 | 106 | }*/ |
antbig | 0:ad97421fb1fb | 107 | |
antbig | 0:ad97421fb1fb | 108 | instruction.order = charToInstructionType(instructionOrder); |
antbig | 0:ad97421fb1fb | 109 | instruction.direction = charToInstructionDirection(instructionDirection); |
antbig | 0:ad97421fb1fb | 110 | instruction.precision = charToInstructionPrecisionOuRecalage(instructionPrecision); |
antbig | 0:ad97421fb1fb | 111 | instruction.nextActionType = charToInstructionNextActionType(instructionNextActionType); |
antbig | 0:ad97421fb1fb | 112 | instruction.jumpAction = charToInstructionNextActionJumpType(instructionJumpAction); |
antbig | 0:ad97421fb1fb | 113 | |
antbig | 0:ad97421fb1fb | 114 | |
antbig | 0:ad97421fb1fb | 115 | return instruction; |
antbig | 0:ad97421fb1fb | 116 | } |
antbig | 0:ad97421fb1fb | 117 | |
antbig | 4:88431b537477 | 118 | /****************************************************************************************/ |
antbig | 4:88431b537477 | 119 | /* FUNCTION NAME: loadAllInstruction */ |
antbig | 4:88431b537477 | 120 | /* DESCRIPTION : Charger toutes les instructions du fichier de stratégie */ |
antbig | 4:88431b537477 | 121 | /* Il faut utiliser strcpy(cheminFileStart,"/local/strat.txt"); */ |
antbig | 4:88431b537477 | 122 | /* pour indiquer le fichier à utiliser */ |
antbig | 4:88431b537477 | 123 | /****************************************************************************************/ |
antbig | 0:ad97421fb1fb | 124 | void loadAllInstruction(void) { |
antbig | 0:ad97421fb1fb | 125 | |
antbig | 0:ad97421fb1fb | 126 | struct S_Instruction instruction; |
antbig | 0:ad97421fb1fb | 127 | char LineBuffer[SIZE]; |
antbig | 0:ad97421fb1fb | 128 | printf("Reading file : "); |
antbig | 0:ad97421fb1fb | 129 | printf(cheminFileStart); |
antbig | 0:ad97421fb1fb | 130 | printf("\n"); |
antbig | 0:ad97421fb1fb | 131 | FILE *testFile = fopen(cheminFileStart, "rt"); //Ouverture du fichier en mode lecture seul au format string |
antbig | 0:ad97421fb1fb | 132 | |
antbig | 0:ad97421fb1fb | 133 | nb_instructions = 0; |
antbig | 0:ad97421fb1fb | 134 | while (fgets(LineBuffer, SIZE, testFile) != NULL) { |
antbig | 0:ad97421fb1fb | 135 | instruction = stringToInstruction(LineBuffer); |
antbig | 0:ad97421fb1fb | 136 | strat_instructions[nb_instructions] = instruction; |
antbig | 1:116040d14164 | 137 | if(strat_instructions[nb_instructions].order == UNKNOWN) { |
antbig | 1:116040d14164 | 138 | errorInstructionLoop();//L'instruction est pas bonne !! |
antbig | 1:116040d14164 | 139 | } |
antbig | 0:ad97421fb1fb | 140 | //printf(LineBuffer); |
antbig | 0:ad97421fb1fb | 141 | //debug_Instruction(instruction); |
antbig | 0:ad97421fb1fb | 142 | nb_instructions++; |
antbig | 0:ad97421fb1fb | 143 | } |
antbig | 0:ad97421fb1fb | 144 | printf("nb instruction = %d\n",nb_instructions); |
antbig | 0:ad97421fb1fb | 145 | fclose(testFile); |
antbig | 0:ad97421fb1fb | 146 | |
antbig | 0:ad97421fb1fb | 147 | } |
antbig | 4:88431b537477 | 148 | |
antbig | 4:88431b537477 | 149 | /****************************************************************************************/ |
antbig | 4:88431b537477 | 150 | /* FUNCTION NAME: FileExists */ |
antbig | 4:88431b537477 | 151 | /* DESCRIPTION : Permet de vérifier si un fichier existe */ |
antbig | 4:88431b537477 | 152 | /****************************************************************************************/ |
antbig | 4:88431b537477 | 153 | int FileExists(const char *fname) |
antbig | 4:88431b537477 | 154 | { |
antbig | 4:88431b537477 | 155 | FILE *file; |
antbig | 4:88431b537477 | 156 | if (file = fopen(fname, "r")) |
antbig | 4:88431b537477 | 157 | { |
antbig | 4:88431b537477 | 158 | fclose(file); |
antbig | 4:88431b537477 | 159 | return 1; |
antbig | 4:88431b537477 | 160 | } |
antbig | 4:88431b537477 | 161 | return 0; |
antbig | 4:88431b537477 | 162 | } |