Created
December 31, 2018 07:37
-
-
Save RQF7/78627402260290c824418a8d0923bccd to your computer and use it in GitHub Desktop.
Generator of programs for CULET. http://www.comunidad.escom.ipn.mx/ALIROB/CULET
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* compiler.cpp | |
* Generator of programs for the head (rotator cube). | |
* CULET: a Turing machine with Cubelets and Lego. | |
* | |
* Dependencies: | |
* + jsoncpp: https://github.com/open-source-parsers/jsoncpp | |
*/ | |
#include <fstream> | |
#include <iostream> | |
#include <json/value.h> | |
#include <json/reader.h> | |
#include <map> | |
#include <string> | |
using namespace std; | |
using namespace Json; | |
/** | |
* Main function. Receives as arguments: | |
* 1. .json file with the machine description. | |
* 2. .c name of destiny file. | |
*/ | |
int main (int num_args, char **args) | |
{ | |
/* Read file with description of the machine. */ | |
ifstream file_with_rules {string{args[1]}}; | |
string contents, line; | |
while(getline(file_with_rules, line)) | |
contents += line; | |
file_with_rules.close(); | |
Value root; | |
Reader lector; | |
lector.parse(contents, root); | |
ofstream destiny_file {string{args[2]}}; | |
map<string, int> states; | |
for (int i = 0; i < root["states"].size(); i++) | |
states.insert(make_pair(root["states"][i].asString(), i)); | |
/* Firs block of program. */ | |
destiny_file << "#include \"cubelet.h\"" << endl << endl | |
<< "#define DOWN_SENSOR 83755" << endl | |
<< "#define UP_SENSOR 88467" << endl | |
<< "#define THRESHOLD_CERCANY 250" << endl | |
<< "#define DESCENT_TIME_ONE 1200" << endl | |
<< "#define DESCENT_TIME_CERO 700" << endl | |
<< "#define POWER_ROTATOR 76" << endl | |
<< "#define POWER_ROTATOR_RISE 75" << endl << endl | |
<< "int state;" << endl | |
<< "int state_machine;" << endl | |
<< "int direction;" << endl << endl | |
<< "#define RIGHT 1" << endl | |
<< "#define LEFT 2" << endl << endl | |
<< "void setup()" << endl | |
<< "{" << endl | |
<< " block_value = 0;" << endl | |
<< " state = 0;" << endl | |
<< " state_machine = " << states[root["initial_state"].asString()] | |
<< ";" << endl | |
<< "}" << endl << endl | |
<< "void loop()" << endl | |
<< "{" << endl | |
<< " switch (state){" << endl | |
<< " case 0:" << endl | |
<< " wait(2000);" << endl | |
<< " state = 1;" << endl | |
<< " break;" << endl | |
<< " case 1:" << endl | |
<< " if (get_block_value(DOWN_SENSOR) < 128) {" << endl | |
<< " switch (state_machine) {" << endl; | |
/* First set of rules. */ | |
for (auto rule : root["rules"]) | |
if (rule["symbol"].asString() == "1") | |
destiny_file | |
<< " case " << states[rule["state"].asString()] | |
<< ":" << endl | |
<< " state_machine = " | |
<< states[rule["new_state"].asString()] | |
<< ";" << endl | |
<< " state = " | |
<< (rule["new_symbol"].asString() == "1" ? "40" : "60") | |
<< ";" << endl | |
<< " direction = " | |
<< (rule["direction"].asString() == "R" ? "RIGHT" : "LEFT") | |
<< ";" << endl | |
<< " break;" << endl; | |
/* Second block of program. */ | |
destiny_file << " }" << endl | |
<< " } else {" << endl | |
<< " switch (state_machine) {" << endl; | |
/* Second set of rules. */ | |
for (auto rule : root["rules"]) | |
if (rule["symbol"].asString() == "0") | |
destiny_file | |
<< " case " << states[rule["state"].asString()] | |
<< ":" << endl | |
<< " state_machine = " | |
<< states[rule["new_state"].asString()] | |
<< ";" << endl | |
<< " state = " | |
<< (rule["new_symbol"].asString() == "0" ? "40" : "50") | |
<< ";" << endl | |
<< " direction = " | |
<< (rule["direction"].asString() == "R" ? "RIGHT" : "LEFT") | |
<< ";" << endl | |
<< " break;" << endl; | |
/* Third block of program. */ | |
destiny_file << " }" << endl | |
<< " }" << endl | |
<< " break;" << endl | |
<< " case 40: " << endl | |
<< " if (direction == RIGHT) { " << endl | |
<< " state = 41; " << endl | |
<< " } else { " << endl | |
<< " state = 42; " << endl | |
<< " } " << endl | |
<< " break; " << endl | |
<< " case 41: " << endl | |
<< " block_value = 1; " << endl | |
<< " if (get_block_value(DOWN_SENSOR) >= THRESHOLD_CERCANY) { " << endl | |
<< " state = 43; " << endl | |
<< " } " << endl | |
<< " break; " << endl | |
<< " case 42: " << endl | |
<< " block_value = 2; " << endl | |
<< " if (get_block_value(DOWN_SENSOR) >= THRESHOLD_CERCANY) { " << endl | |
<< " state = 43; " << endl | |
<< " } " << endl | |
<< " break; " << endl | |
<< " case 43: " << endl | |
<< " if (get_block_value(DOWN_SENSOR) < THRESHOLD_CERCANY) { " << endl | |
<< " if (direction == LEFT)" << endl | |
<< " wait(200);" << endl | |
<< " else" << endl | |
<< " wait(100);" << endl | |
<< " block_value = 0; " << endl | |
<< " wait(200); " << endl | |
<< " state = 1; " << endl | |
<< " } " << endl | |
<< " break; " << endl | |
<< " case 50:" << endl | |
<< " set_rotate_direction(FORWARD);" << endl | |
<< " set_rotate(POWER_ROTATOR);" << endl | |
<< " wait(DESCENT_TIME_ONE);" << endl | |
<< " set_rotate(0);" << endl | |
<< " wait(500);" << endl | |
<< " state = 51;" << endl | |
<< " break;" << endl | |
<< " case 51:" << endl | |
<< " set_rotate_direction(BACKWARD);" << endl | |
<< " set_rotate(POWER_ROTATOR_RISE);" << endl | |
<< " if (get_block_value(UP_SENSOR) > 128) {" << endl | |
<< " set_rotate(0);" << endl | |
<< " wait(500);" << endl | |
<< " state = 40;" << endl | |
<< " }" << endl | |
<< " break;" << endl | |
<< " case 60:" << endl | |
<< " set_rotate_direction(BACKWARD);" << endl | |
<< " set_rotate(POWER_ROTATOR);" << endl | |
<< " wait(DESCENT_TIME_CERO);" << endl | |
<< " set_rotate(0);" << endl | |
<< " wait(500);" << endl | |
<< " state = 61;" << endl | |
<< " break;" << endl | |
<< " case 61:" << endl | |
<< " set_rotate_direction(FORWARD);" << endl | |
<< " set_rotate(POWER_ROTATOR_RISE);" << endl | |
<< " if (get_block_value(UP_SENSOR) > 128) {" << endl | |
<< " set_rotate(0);" << endl | |
<< " wait(500);" << endl | |
<< " state = 40;" << endl | |
<< " }" << endl | |
<< " break;" << endl | |
<< " }" << endl | |
<< "}" << endl; | |
destiny_file.close(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment