code petit robot pour homologation
Fork of CRAC-Strat_2017_V2 by
Instruction/Instruction.cpp@1:116040d14164, 2016-04-15 (annotated)
- Committer:
- antbig
- Date:
- Fri Apr 15 10:49:40 2016 +0000
- Revision:
- 1:116040d14164
- Parent:
- 0:ad97421fb1fb
- Child:
- 4:88431b537477
Premier test fonctionnel avec le petit robot,
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 | 0:ad97421fb1fb | 69 | |
antbig | 0:ad97421fb1fb | 70 | struct S_Instruction stringToInstruction(char line[]) { |
antbig | 0:ad97421fb1fb | 71 | struct S_Instruction instruction; |
antbig | 0:ad97421fb1fb | 72 | |
antbig | 0:ad97421fb1fb | 73 | char instructionOrder; |
antbig | 0:ad97421fb1fb | 74 | char instructionDirection; |
antbig | 0:ad97421fb1fb | 75 | char instructionPrecision; |
antbig | 0:ad97421fb1fb | 76 | char instructionNextActionType; |
antbig | 0:ad97421fb1fb | 77 | char instructionJumpAction; |
antbig | 1:116040d14164 | 78 | int errorCode = 0; |
antbig | 0:ad97421fb1fb | 79 | /* |
antbig | 0:ad97421fb1fb | 80 | Info sur la fonction sscanf |
antbig | 0:ad97421fb1fb | 81 | %d -> Entier signé |
antbig | 0:ad97421fb1fb | 82 | %u -> Entié non signé |
antbig | 0:ad97421fb1fb | 83 | %c -> char |
antbig | 0:ad97421fb1fb | 84 | */ |
antbig | 1:116040d14164 | 85 | errorCode = sscanf(line, "%d,%c,%c,%u,%u,%d,%c,%c,%c,%u,%u,%d,%d", |
antbig | 0:ad97421fb1fb | 86 | &instruction.lineNumber, |
antbig | 0:ad97421fb1fb | 87 | &instructionOrder, |
antbig | 0:ad97421fb1fb | 88 | &instructionDirection, |
antbig | 0:ad97421fb1fb | 89 | &instruction.arg1, |
antbig | 0:ad97421fb1fb | 90 | &instruction.arg2, |
antbig | 0:ad97421fb1fb | 91 | &instruction.arg3, |
antbig | 0:ad97421fb1fb | 92 | &instructionPrecision, |
antbig | 0:ad97421fb1fb | 93 | &instructionNextActionType, |
antbig | 0:ad97421fb1fb | 94 | &instructionJumpAction, |
antbig | 0:ad97421fb1fb | 95 | &instruction.JumpTimeOrX, |
antbig | 0:ad97421fb1fb | 96 | &instruction.JumpY, |
antbig | 0:ad97421fb1fb | 97 | &instruction.nextLineOK, |
antbig | 0:ad97421fb1fb | 98 | &instruction.nextLineError |
antbig | 0:ad97421fb1fb | 99 | ); |
antbig | 1:116040d14164 | 100 | /* |
antbig | 1:116040d14164 | 101 | if(errorCode != 13) { |
antbig | 1:116040d14164 | 102 | errorInstructionLoop();//L'instruction est pas bonne !! |
antbig | 1:116040d14164 | 103 | }*/ |
antbig | 0:ad97421fb1fb | 104 | |
antbig | 0:ad97421fb1fb | 105 | instruction.order = charToInstructionType(instructionOrder); |
antbig | 0:ad97421fb1fb | 106 | instruction.direction = charToInstructionDirection(instructionDirection); |
antbig | 0:ad97421fb1fb | 107 | instruction.precision = charToInstructionPrecisionOuRecalage(instructionPrecision); |
antbig | 0:ad97421fb1fb | 108 | instruction.nextActionType = charToInstructionNextActionType(instructionNextActionType); |
antbig | 0:ad97421fb1fb | 109 | instruction.jumpAction = charToInstructionNextActionJumpType(instructionJumpAction); |
antbig | 0:ad97421fb1fb | 110 | |
antbig | 0:ad97421fb1fb | 111 | |
antbig | 0:ad97421fb1fb | 112 | return instruction; |
antbig | 0:ad97421fb1fb | 113 | } |
antbig | 0:ad97421fb1fb | 114 | |
antbig | 0:ad97421fb1fb | 115 | void loadAllInstruction(void) { |
antbig | 0:ad97421fb1fb | 116 | |
antbig | 0:ad97421fb1fb | 117 | struct S_Instruction instruction; |
antbig | 0:ad97421fb1fb | 118 | char LineBuffer[SIZE]; |
antbig | 0:ad97421fb1fb | 119 | printf("Reading file : "); |
antbig | 0:ad97421fb1fb | 120 | printf(cheminFileStart); |
antbig | 0:ad97421fb1fb | 121 | printf("\n"); |
antbig | 0:ad97421fb1fb | 122 | FILE *testFile = fopen(cheminFileStart, "rt"); //Ouverture du fichier en mode lecture seul au format string |
antbig | 0:ad97421fb1fb | 123 | |
antbig | 0:ad97421fb1fb | 124 | nb_instructions = 0; |
antbig | 0:ad97421fb1fb | 125 | while (fgets(LineBuffer, SIZE, testFile) != NULL) { |
antbig | 0:ad97421fb1fb | 126 | instruction = stringToInstruction(LineBuffer); |
antbig | 0:ad97421fb1fb | 127 | strat_instructions[nb_instructions] = instruction; |
antbig | 1:116040d14164 | 128 | if(strat_instructions[nb_instructions].order == UNKNOWN) { |
antbig | 1:116040d14164 | 129 | errorInstructionLoop();//L'instruction est pas bonne !! |
antbig | 1:116040d14164 | 130 | } |
antbig | 0:ad97421fb1fb | 131 | //printf(LineBuffer); |
antbig | 0:ad97421fb1fb | 132 | //debug_Instruction(instruction); |
antbig | 0:ad97421fb1fb | 133 | nb_instructions++; |
antbig | 0:ad97421fb1fb | 134 | } |
antbig | 0:ad97421fb1fb | 135 | printf("nb instruction = %d\n",nb_instructions); |
antbig | 0:ad97421fb1fb | 136 | fclose(testFile); |
antbig | 0:ad97421fb1fb | 137 | |
antbig | 0:ad97421fb1fb | 138 | } |