# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"\b(?:if|for|while|else|else\s+?if|switch)\b"
test_str = ("#include \"main.h\"\n"
"#include \"chassis.h\"\n"
"#include <cmath>\n"
"#include <iostream>\n\n"
"//Exponential Drive Function\n"
"int exponential (int joystickVal, float driveExp, int joyDead, int motorMin) { //e\n"
" int joySign;\n"
" int joyMax = 128 - joyDead;\n"
" int joyLive = abs(joystickVal) - joyDead;\n"
" if (joystickVal > 0) {joySign = 1;}\n"
" else if (joystickVal < 0) {joySign = -1;}\n"
" else {joySign = 0;}\n"
" int power = joySign * (motorMin + ((127 - motorMin) * (pow(joyLive, driveExp) / pow(joyMax, driveExp))));\n"
" return power;\n"
"}\n\n\n"
"void Chassis::OP_Chassis(void){\n"
" int Ch3 = exponential(master.get_analog(E_CONTROLLER_ANALOG_LEFT_X), 1.5 /*DriveExp*/, 8 /*JoyDead*/, 15 /*MotorMin*/);\n"
" int Ch1 = exponential(master.get_analog(E_CONTROLLER_ANALOG_RIGHT_X), 1.5 /*DriveExp*/, 8 /*JoyDead*/, 15 /*MotorMin*/);\n"
" int Ch4 = exponential(master.get_analog(E_CONTROLLER_ANALOG_LEFT_Y), 1.5 /*DriveExp*/, 8 /*JoyDead*/, 15 /*MotorMin*/);\n"
" /*BaseFL.move(-Ch3 - Ch1);\n"
" BaseBL.move(-Ch3 - Ch1);\n"
" BaseFR.move(Ch3 - Ch1);\n"
" BaseBR.move(Ch3 - Ch1);*/\n\n"
" BaseFL.move(-Ch3 - Ch1 - Ch4);\n"
" BaseFR.move(-Ch3 - Ch1 + Ch4);\n"
" BaseBL.move(Ch3 - Ch1 - Ch4);\n"
" BaseBR.move(Ch3 - Ch1 + Ch4);\n\n"
" /*\n"
" ChassisMotors[BASE_FL_PORT].move(-Ch3 - Ch1 - Ch4);\n"
" ChassisMotors[BASE_FR_PORT].move(-Ch3 - Ch1 + Ch4);\n"
" ChassisMotors[BASE_BL_PORT].move(Ch3 - Ch1 - Ch4);\n"
" ChassisMotors[BASE_BR_PORT].move(Ch3 - Ch1 + Ch4);\n"
" */\n"
"}\n\n"
"void Chassis::stopDriving(void){\n"
" BaseFL.move(0);\n"
" BaseFR.move(0);\n"
" BaseBL.move(0);\n"
" BaseBR.move(0);\n"
"}\n\n"
"void Chassis::TurnDistance(int direction, int targetValue, int timeout){\n\n"
" float driveKP = 0.7;\n"
" float driveKD = 1.2;\n"
" //same PID Logic as turnValue\n"
" int motorPower;\n"
" int startTime = millis();\n"
" int currentValue = 0;\n"
" int err = 0;\n"
" int derr = 0;\n"
" int err_last = 0;\n"
" int err_sum = 0;\n"
" float KI = 0;\n"
" float p;\n"
" float i = 0;\n"
" float d;\n"
" direction == DIRECTION_LEFT ? BaseFL.tare_position() : BaseFR.tare_position(); //resets left encoder if turning left, right if turning right\n"
" while((millis() - startTime) < timeout){\n\n"
" currentValue = abs(direction == DIRECTION_LEFT ? BaseFL.get_position() : BaseFR.get_position()); //gets left encoder if turning left, right if turning right\n"
" err = targetValue - currentValue;\n"
" err_last = err;\n"
" derr = (err - err_last);\n"
" p = (driveKP * err);\n"
" err_sum += err;\n"
" d = driveKD * derr;\n"
" motorPower = p+i+d;\n\n"
" /* if(motorPower > 90){motorPower = 90;} //cap speed at 90\n"
" if(motorPower < -90){motorPower = -90;} */\n"
" motorPower = (motorPower > 90 ? 90 : motorPower < -90 ? -90 : motorPower); //cap speed at +- 90 BUG : Needs test\n"
" BaseFL.move(direction*motorPower);\n"
" BaseFR.move((direction*motorPower));\n"
" BaseBL.move((direction*motorPower));\n"
" BaseBR.move(direction*motorPower);\n\n"
" delay(20);\n\n"
" }\n"
"}\n"
"void Chassis::MoveDistance(int direction, int targetValue, int timeout, int cap){\n\n"
" float driveKP = 0.7;\n"
" float driveKD = 1.2;\n"
" //same PID Logic as turnValue\n"
" int motorPower;\n"
" int startTime = millis();\n"
" int currentValue = 0;\n"
" int err = 0;\n"
" int derr = 0;\n"
" int err_last = 0;\n"
" int err_sum = 0;\n"
" float KI = 0;\n"
" float p;\n"
" float i = 0;\n"
" float d;\n"
" BaseFL.tare_position();\n"
" while((millis() - startTime) < timeout){\n\n"
" currentValue = abs(BaseFL.get_position());\n"
" err = targetValue - currentValue;\n"
" err_last = err;\n"
" derr = (err - err_last);\n"
" p = (driveKP * err);\n"
" err_sum += err;\n"
" d = driveKD * derr;\n\n"
" motorPower = p+i+d;\n\n"
" if(motorPower > cap){motorPower = cap;}\n"
" if(motorPower < -cap){motorPower = -cap;}\n"
" // motorPower = (motorPower > 1 ? 1 : motorPower < -1 ? -1 : motorPower);\n"
" BaseFL.move(-direction*motorPower);\n"
" BaseFR.move((direction*motorPower));\n"
" BaseBL.move((-direction*motorPower));\n"
" BaseBR.move(direction*motorPower);\n\n"
" delay(20);\n\n"
" }\n"
"}\n"
"void Chassis::TurnGyro(int direction, int targetValue, int timeout){\n\n"
" float driveKP = 1.2;\n"
" float driveKD = 0.8;\n\n"
" gyro.reset();\n"
" //initialize variables\n"
" int motorPower; //motor power level\n"
" int startTime = millis(); //Elapsed time since start of the sequence\n"
" int currentValue = 0; //starting value of 0\n"
" int turn_err = 0; //error value init\n"
" int derr = 0;//error difference\n"
" int err_last = 0; //last error\n"
" int err_sum = 0; //sum of errors\n"
" float KI = 0; //KI value - not currently used'\n"
" float p; //p value normally 0.8\n"
" float i = 0; //I value\n"
" float d; //d value normally 0.7\n\n"
" while((millis() - startTime) < timeout){\n"
" currentValue = gyro.get_value();\n"
" turn_err = targetValue - currentValue; //error is how far the current position is from the position you put into the loop\n"
" err_last = turn_err; //stores the error\n"
" derr = (turn_err - err_last); //difference between how far you were from the target last sequence compared to this sequence\n"
" p = (driveKP * turn_err); //p value is preset driveKP multiplied by how far we are from our target\n"
" err_sum += turn_err; //err_sum increases by the sum of errors\n"
" d = driveKD * derr; //d value is preset driveKD multiplied by the difference between how far you were from the last rotation\n\n"
" motorPower = p+i+d; //motorpower is the sum of p, i, and d\n\n"
" if(motorPower > 90){motorPower = 90;} //if the motor power is greater than 127 (the maximun it can go), set it to 127\n"
" if(motorPower < -90){motorPower = -90;}//if the motor power is less than -127 (the minimum it can go), set it to -127\n\n"
" /*\n"
" * Move motors the motorpower value times the direction.\n"
" * Here, the Front Left motor and the Back Left motor are moving backwards if direction == 1\n"
" * and the Back Right and Front Right motors are moving forwards if direction ==1\n"
" * this is the setup that allows the base to turn\n"
" */\n"
" BaseFL.move(direction*motorPower);\n"
" BaseBL.move(direction*motorPower);\n"
" BaseBR.move((direction*motorPower));\n"
" BaseFR.move((direction*motorPower));\n\n"
" delay(20);\n"
" }\n\n"
"}\n"
"void Chassis::StrafeDistance(int direction, int targetValue, int timeout){\n\n"
" float driveKP = 0.7;\n"
" float driveKD = 1.2;\n"
" //same PID Logic as turnValue\n"
" int motorPower;\n"
" int startTime = millis();\n"
" int currentValue = 0;\n"
" int err = 0;\n"
" int derr = 0;\n"
" int err_last = 0;\n"
" int err_sum = 0;\n"
" float KI = 0;\n"
" float p;\n"
" float i = 0;\n"
" float d;\n"
" direction == DIRECTION_LEFT ? BaseFL.tare_position() : BaseFR.tare_position(); //resets left encoder if turning left, right if turning right\n"
" while((millis() - startTime) < timeout){\n\n"
" currentValue = abs(direction == DIRECTION_LEFT ? BaseFL.get_position() : BaseFR.get_position()); //gets left encoder if turning left, right if turning right\n"
" err = targetValue - currentValue;\n"
" err_last = err;\n"
" derr = (err - err_last);\n"
" p = (driveKP * err);\n"
" err_sum += err;\n"
" d = driveKD * derr;\n\n"
" motorPower = p+i+d;\n\n"
" /* if(motorPower > 90){motorPower = 90;} //cap speed at 90\n"
" if(motorPower < -90){motorPower = -90;} */\n"
" motorPower = (motorPower > 90 ? 90 : motorPower < -90 ? -90 : motorPower); //cap speed at +- 90 BUG : Needs test\n"
" BaseFL.move(direction*motorPower);\n"
" BaseFR.move((direction*motorPower));\n"
" BaseBL.move(-(direction*motorPower));\n"
" BaseBR.move(-direction*motorPower);\n\n"
" delay(20);\n\n"
" }\n"
"}\n"
"void Chassis::driveOnAngle(double angle, int quadrant, int targetValue, int timeout){\n\n"
" //PD Constants\n"
" float driveKP = 0.7;\n"
" float driveKD = 1.2;\n\n"
" //General variables\n"
" int motorPower;\n"
" int startTime = millis();\n"
" int currentValue = 0;\n"
" int err = 0;\n"
" int derr = 0;\n"
" int err_last = 0;\n"
" int err_sum = 0;\n"
" float KI = 0;\n"
" float p = 0;\n"
" float i = 0;\n"
" float d = 0;\n\n"
" //Wheel modification values\n"
" double FRMod;\n"
" double FLMod;\n"
" double BLMod;\n"
" double BRMod;\n\n"
" BaseFL.tare_position();\n"
" BaseFR.tare_position();\n"
" BaseBL.tare_position();\n"
" BaseBR.tare_position();\n"
" //Convert degree to radians\n"
" // angle = angle * (M_PI / 180);\n\n"
" //Determine which qaudrant we're driving in\n"
" /*\n"
" if (angle < (M_PI / 2) && angle > 0 ) {\n"
" quadrant = QUADRANT_1;\n"
" }\n"
" else if(angle < (M_PI) && angle > (M_PI / 2)) {\n"
" quadrant = QUADRANT_2;\n"
" }\n"
" else if((angle < ((3*M_PI) / 2)) && angle > (M_PI)) {\n"
" quadrant = QUADRANT_3;\n"
" }\n"
" else if(angle < (2*M_PI) && angle > (((3*M_PI) / 2))){\n"
" quadrant = QUADRANT_4;\n"
" }\n"
"*/\n"
" while((millis() - startTime) < timeout){\n\n"
" switch (quadrant) { //Change wheel modifications and enccder values based on the quadrant we're driving in.\n\n"
" case QUADRANT_1 : //If we're driving in quadrant 1 (0 to π/2)\n"
" currentValue = abs(BaseFR.get_position());\n"
" FRMod = sin(angle - (M_PI/4));\n"
" FLMod = -1;\n"
" BLMod = -sin(angle - (M_PI / 4));\n"
" BRMod = 1;\n\n"
" case QUADRANT_2 : //If we're driving in quadrant 1 (π/2 to π)\n"
" currentValue = abs(BaseFL.get_position());\n"
" FLMod = 1;\n"
" FRMod = sin(angle - ((3*M_PI)/4));\n"
" BRMod = 1;\n"
" BLMod = sin(angle - ((3*M_PI)/4));\n\n"
" case QUADRANT_3 : //BUG FL, BR might be Negative as well\n"
" currentValue = abs(BaseBL.get_position());\n"
" FLMod = 1;\n"
" BLMod = -(sin(angle - (M_PI)/4));\n"
" BRMod = -1;\n"
" FRMod = (sin (angle - ((M_PI)/4)));\n\n\n"
" case QUADRANT_4:\n"
" /*BACK LEFT ALIGNMENT*/\n"
" currentValue = abs(BaseBL.get_position());\n"
" FLMod = 1;\n"
" BLMod = -(sin(angle - (M_PI)/4));\n"
" BRMod = -1;\n"
" FRMod = (sin (angle - ((M_PI)/4)));\n"
" }\n\n\n"
" err = targetValue - currentValue; //error is how far the current position is from the position you put into the loop\n"
" err_last = err; //stores the error\n"
" derr = (err - err_last); //difference between how far you were from the target last sequence compared to this sequence\n"
" p = (driveKP * err); //p value is preset driveKP multiplied by how far we are from our target\n"
" err_sum += err; //err_sum increases by the sum of errors\n"
" d = driveKD * derr; //d value is preset driveKD multiplied by the difference between how far you were from the last rotation\n\n"
" motorPower = p+i+d; //motorpower is the sum of p, i, and d\n\n"
" if(motorPower > 90){motorPower = 90;} //if the motor power is greater than 127 (the maximun it can go), set it to 127\n"
" if(motorPower < -90){motorPower = -90;}//if the motor power is less than -127 (the minimum it can go), set it to -127\n\n"
" //moves the wheels according to the PID output and the Quadrant Modifications\n"
" BaseFL.move(motorPower * FLMod);\n"
" BaseBL.move(motorPower * BLMod);\n"
" BaseBR.move(motorPower * BRMod);\n"
" BaseFR.move(motorPower * FRMod);\n\n"
" delay(20);\n"
" }\n\n"
"}")
matches = re.finditer(regex, test_str)
for matchNum, match in enumerate(matches, start=1):
print ("Match {matchNum} was found at {start}-{end}: {match}".format(matchNum = matchNum, start = match.start(), end = match.end(), match = match.group()))
for groupNum in range(0, len(match.groups())):
groupNum = groupNum + 1
print ("Group {groupNum} found at {start}-{end}: {group}".format(groupNum = groupNum, start = match.start(groupNum), end = match.end(groupNum), group = match.group(groupNum)))
# Note: for Python 2.7 compatibility, use ur"" to prefix the regex and u"" to prefix the test string and substitution.
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Python, please visit: https://docs.python.org/3/library/re.html