/** * @license Licensed under the Apache License, Version 2.0 (the "License"): * http://www.apache.org/licenses/LICENSE-2.0 */ /** * @fileoverview Code generator for the test 2 blocks. */ 'use strict'; goog.provide('Blockly.Python.motor'); goog.require('Blockly.Python'); // Blockly.Python['ultrasonicavoidingrobot'] = function(block) { // var text_uarechopin = block.getFieldValue('uarEchoPin'); // var text_uartrigpin = block.getFieldValue('uarTrigPin'); // var number_uarspeed = block.getFieldValue('uarSpeed'); // var number_uardistance = block.getFieldValue('uarDistance'); // var uarMovementEntity; // var uarDeclaration = '#include \n' + // '#define stA ' + text_uartrigpin + '\n' + // '#define seA ' + text_uarechopin + '\n' + // '#define MAX_DISTANCE 100\n' + // 'int PWMA = 5;\n' + //speed control // 'int PWMB = 6;\n' + // 'int AIN2 = 10;\n' + //direction // 'int AIN1 = 9;\n' + // 'int BIN1 = 4;\n' + //direction // 'int BIN2 = 8;\n' + // 'int RA = 0;\n' + //init rate // 'int RB = 0;\n' + // 'int STBY = A5;\n' + //stand-by // 'int carSpeedSonar;\n' + // 'volatile int state = LOW;\n' + // 'NewPing sonarHead(stA, seA, MAX_DISTANCE);\n'; // var uarSetup = 'pinMode(STBY, OUTPUT);\n' + // 'pinMode(PWMA, OUTPUT);\n' + // 'pinMode(AIN1, OUTPUT);\n' + // 'pinMode(AIN2, OUTPUT);\n' + // 'pinMode(PWMB, OUTPUT);\n' + // 'pinMode(BIN1, OUTPUT);\n' + // 'pinMode(BIN2, OUTPUT);\n' + // 'Serial.begin(115200);\n'; // var uarStop = 'void Stop() {\n' + // 'move(1, 0, 2); //motor 1, full speed, left\n' + // 'move(2, 0, 2); //motor 2, full speed, left\n' + // '}\n'; // var uarMove = 'void move(int motor, int speed, int direction)\n' + // '{\n' + // ' digitalWrite(STBY, HIGH); //disable standby\n' + // '\n' + // ' boolean inPin1 = LOW;\n' + // ' boolean inPin2 = HIGH;\n' + // '\n' + // ' if (direction == 1) {\n' + // ' inPin1 = HIGH;\n' + // ' inPin2 = LOW;\n' + // ' }\n' + // '\n' + // ' if (motor == 1) {\n' + // ' digitalWrite(AIN1, inPin1);\n' + // ' digitalWrite(AIN2, inPin2);\n' + // ' analogWrite(PWMA, speed);\n' + // ' } else {\n' + // ' digitalWrite(BIN1, inPin1);\n' + // ' digitalWrite(BIN2, inPin2);\n' + // ' analogWrite(PWMB, speed);\n' + // ' }\n' + // '}\n'; // Blockly.Python.addDeclaration('uarDeclaration', uarDeclaration); // Blockly.Python.addSetup('uarSetupn', uarSetup); // Blockly.Python.addFunction('uarStop', uarStop); // Blockly.Python.addFunction('uarMove', uarMove); // var code = 'int dist = sonarHead.ping_cm();\n' + // 'int threshold = ' + number_uardistance + '; \n' + // '// adjustable\n' + // 'carSpeedSonar = ' + number_uarspeed + '; \n' + // 'move(1, carSpeedSonar, 0); //motor 1, full speed, left\n' + // 'move(0, carSpeedSonar, 0); //motor 2, full speed, left\n' + // 'if ( dist < threshold && dist > 0 ) {\n'; // return code; // }; // Blockly.Python['uardirectionforward'] = function(block) { // var number_uardirduration = block.getFieldValue('uarDirDuration'); // var uarDurationSec = number_uardirduration * 1000; // // TODO: Assemble JavaScript into code variable. // var code = 'move(1, carSpeedSonar, 0); //motor 1, full speed, left\n' + // 'move(0, carSpeedSonar, 0); //motor 2, full speed, left\n' + // 'delay(' + uarDurationSec + ');\n'; // return code; // }; // Blockly.Python['uardirectionbackward'] = function(block) { // var number_uardirduration = block.getFieldValue('uarDirDuration'); // var uarDurationSec = number_uardirduration * 1000; // // TODO: Assemble JavaScript into code variable. // var code = 'move(1, carSpeedSonar, 1); //motor 1, full speed, left\n' + // 'move(0, carSpeedSonar, 1); //motor 2, full speed, left\n' + // 'delay(' + uarDurationSec + ');\n'; // return code; // }; // Blockly.Python['uardirectionleft'] = function(block) { // var number_uardirduration = block.getFieldValue('uarDirDuration'); // var uarDurationSec = number_uardirduration * 1000; // // TODO: Assemble JavaScript into code variable. // var code = 'move(1, carSpeedSonar, 1); //motor 1, full speed, left\n' + // 'move(0, carSpeedSonar, 0); //motor 2, full speed, left\n' + // 'delay(' + uarDurationSec + ');\n'; // return code; // }; // Blockly.Python['uardirectionright'] = function(block) { // var number_uardirduration = block.getFieldValue('uarDirDuration'); // var uarDurationSec = number_uardirduration * 1000; // // TODO: Assemble JavaScript into code variable. // var code = 'move(1, carSpeedSonar, 0); //motor 1, full speed, left\n' + // 'move(0, carSpeedSonar, 1); //motor 2, full speed, left\n' + // 'delay(' + uarDurationSec + ');\n'; // return code; // }; // Blockly.Python['uarfinishsetup'] = function(block) { // // TODO: Assemble JavaScript into code variable. // var code = '}\n'; // return code; // }; // Blockly.Python['motor_setup'] = function(block) { // var motor_LT_decl = '#define SWITCHA 12\n' + // '#define SWITCHB 13\n' + // 'int LightAnalogValue_A, LightAnalogValue_B;\n'; // Blockly.Python.addDeclaration('motor_LT_decl', motor_LT_decl); // //motor A & B setup // var motor_AB_decl = 'int PWMA = 5;\n' + //speed control // 'int PWMB = 6;\n' + // 'int AIN2 = 10;\n' + //direction // 'int AIN1 = 9;\n' + // 'int BIN1 = 4;\n' + //direction // 'int BIN2 = 8;\n' + // 'int RA = 0;\n' + //init rate // 'int RB = 0;\n' + // 'int STBY = A5;\n' + //stand-by // 'volatile int state = LOW;\n' + // 'int threshold = 500;\n' + //speed control // 'int carSpeed = 100, speedLeft = 100, speedRight = 100;'; // Blockly.Python.addDeclaration('motor_AB_decl', motor_AB_decl); // var motor_AB_setup = 'pinMode(STBY, OUTPUT);\n' + // 'pinMode(PWMA, OUTPUT);\n' + // 'pinMode(AIN1, OUTPUT);\n' + // 'pinMode(AIN2, OUTPUT);\n' + // 'pinMode(PWMB, OUTPUT);\n' + // 'pinMode(BIN1, OUTPUT);\n' + // 'pinMode(BIN2, OUTPUT);\n' + // 'pinMode(A3, INPUT);\n' + // 'pinMode(SWITCHA, INPUT);\n' + // 'pinMode(3, INPUT);\n' + // 'Serial.begin(9600);\n'; // Blockly.Python.addSetup('motor_AB_setup', motor_AB_setup); // var motor_setup = 'gotOne = false;\ngotNew = false;\n' + // 'codeProtocol = UNKNOWN;\n' + // 'codeValue = 0;\n' + // 'Serial.begin(9600);\n' + // 'delay(2000);\n' + // 'myReceiver.enableIRIn();\n'; //Start the receiver // // Blockly.Python.addSetup('motor_setup', motor_setup); // var motor_stop = 'void Stop() {\n' + // ' move(1, 0, 2); //motor 1, full speed, left\n' + // ' move(2, 0, 2); //motor 2, full speed, left\n' + // ' // delay(200);\n' + // '}\n'; // Blockly.Python.addFunction('motor_stop', motor_stop); // var motor_move_code = 'void move(int motor, int speed, int direction)\n' + // '{\n' + // ' digitalWrite(STBY, HIGH); //disable standby\n' + // '\n' + // ' boolean inPin1 = LOW;\n' + // ' boolean inPin2 = HIGH;\n' + // '\n' + // ' if (direction == 1) {\n' + // ' inPin1 = HIGH;\n' + // ' inPin2 = LOW;\n' + // ' }\n' + // '\n' + // ' if (motor == 1) {\n' + // ' digitalWrite(AIN1, inPin1);\n' + // ' digitalWrite(AIN2, inPin2);\n' + // ' analogWrite(PWMA, speed);\n' + // ' } else {\n' + // ' digitalWrite(BIN1, inPin1);\n' + // ' digitalWrite(BIN2, inPin2);\n' + // ' analogWrite(PWMB, speed);\n' + // ' }\n' + // '}\n'; // Blockly.Python.addFunction('motor_move_code', motor_move_code); // var motor_adjustLeft = 'void adjustLeft() {\n' + // ' move(2, speedLeft, 2); \n' + // ' move(1, speedRight, 1); \n' + // ' // delay(100);\n' + // '}\n'; // var motor_adjustRight = 'void adjustRight() {\n' + // ' move(1, speedLeft, 2); \n' + // ' move(2, speedRight, 1);\n' + // ' // delay(100);\n' + // '}\n'; // var motor_goStraight = 'void goStraight() {\n' + // ' move(1, speedLeft, 2); //motor 1, full speed, left\n' + // ' move(2, speedRight, 2); //motor 2, full speed, left\n' + // ' // delay(200);\n' + // '}\n'; // var motor_reverse = 'void reverse() {\n' + // ' move(1, speedLeft, 1);\n' + // ' move(2, speedRight, 1);\n' + // '}\n'; // Blockly.Python.addFunction('motor_adjustLeft', motor_adjustLeft); // Blockly.Python.addFunction('motor_adjustRight', motor_adjustRight); // Blockly.Python.addFunction('motor_goStraight', motor_goStraight); // Blockly.Python.addFunction('motor_reverse', motor_reverse); // return ''; // }; Blockly.Python['motor_setup'] = function() { var stby_def = 'int STBY = A5;'; var motor_A_def = '// Motor A Definition\n' + 'int PWMA = 5; // Speed control\n' + 'int AIN1 = 9; // Direction\n' + 'int AIN2 = 10; // Direction'; var motor_B_def = '// Motor B Definition\n' + 'int PWMB = 6; // Speed control\n' + 'int BIN1 = 4; // Direction\n' + 'int BIN2 = 8; // Direction\n'; Blockly.Python.addDeclaration('STBY_define', stby_def); Blockly.Python.addDeclaration('motor_A_define', motor_A_def); Blockly.Python.addDeclaration('motor_B_define', motor_B_def); var stby_setup = 'pinMode(STBY, OUTPUT);'; var motor_A_setup = '// Motor A Setup\n' + ' pinMode(PWMA, OUTPUT);\n' + ' pinMode(AIN1, OUTPUT);\n' + ' pinMode(AIN2, OUTPUT);'; var motor_B_setup = '// Motor B Setup\n' + ' pinMode(PWMB, OUTPUT);\n' + ' pinMode(BIN1, OUTPUT);\n' + ' pinMode(BIN2, OUTPUT);'; Blockly.Python.addSetup('stby_setup', stby_setup); Blockly.Python.addSetup('motor_A_setup', motor_A_setup); Blockly.Python.addSetup('motor_B_setup', motor_B_setup); var move_func = 'void move(int motor, int speed, int direction) {\n' + ' //Move specific motor at speed and direction\n' + ' //motor: 0 for B 1 for A\n' + ' //speed: 0 is off, and 255 is full speed\n' + ' //direction: 0 clockwise, 1 counter-clockwise\n' + ' digitalWrite(STBY, HIGH); //disable standby\n' + ' boolean inPin1 = LOW;\n' + ' boolean inPin2 = HIGH;\n' + ' if (direction == 1) {\n' + ' inPin1 = HIGH;\n' + ' inPin2 = LOW;\n }\n' + ' if (motor == 1) {\n' + ' digitalWrite(AIN1, inPin1);\n' + ' digitalWrite(AIN2, inPin2);\n' + ' analogWrite(PWMA, speed);\n' + ' } else {\n' + ' digitalWrite(BIN1, inPin1);\n' + ' digitalWrite(BIN2, inPin2);\n' + ' analogWrite(PWMB, speed);\n }\n}'; Blockly.Python.addFunction('motor_move_function', move_func); var stop_func = 'void stop() {\n' + ' //enable standby\n' + ' digitalWrite(STBY, LOW);\n' + '}'; Blockly.Python.addFunction('motor_stop_function', stop_func); return ''; } Blockly.Python['motor_speed'] = function(block) { var number_speed = block.getFieldValue('SPEED'); var code = number_speed; return [code, Blockly.Python.ORDER_NONE]; } Blockly.Python['motorA_move_clockwise'] = function(block) { var dropdown_motor = block.getFieldValue('MOTOR'); var value_speed = Blockly.Python.valueToCode(block, 'SPEED', Blockly.Python.ORDER_ATOMIC); var dropdown_direction = block.getFieldValue('DIRECTION'); var code = 'move('+dropdown_motor+','+value_speed+','+dropdown_direction+');\n'; return code; } Blockly.Python['motorA_move_anticlockwise'] = function(block) { var dropdown_motor = block.getFieldValue('MOTOR'); var value_speed = Blockly.Python.valueToCode(block, 'SPEED', Blockly.Python.ORDER_ATOMIC); var dropdown_direction = block.getFieldValue('DIRECTION'); var code = 'move('+dropdown_motor+','+value_speed+','+dropdown_direction+');\n'; return code; } Blockly.Python['motorB_move_clockwise'] = function(block) { var dropdown_motor = block.getFieldValue('MOTOR'); var value_speed = Blockly.Python.valueToCode(block, 'SPEED', Blockly.Python.ORDER_ATOMIC); var dropdown_direction = block.getFieldValue('DIRECTION'); var code = 'move('+dropdown_motor+','+value_speed+','+dropdown_direction+');\n'; return code; } Blockly.Python['motorB_move_anticlockwise'] = function(block) { var dropdown_motor = block.getFieldValue('MOTOR'); var value_speed = Blockly.Python.valueToCode(block, 'SPEED', Blockly.Python.ORDER_ATOMIC); var dropdown_direction = block.getFieldValue('DIRECTION'); var code = 'move('+dropdown_motor+','+value_speed+','+dropdown_direction+');\n'; return code; } Blockly.Python['motor_stop'] = function(block) { var code = 'stop();\n'; return code; } // //TODO: remote control button setup // Blockly.Python['motor_remote_setup'] = function(block) { // //Blockly.Python.addDeclaration('remote_button_setup', remote_button_decl); // var dropdown_run = block.getFieldValue('run'); // var dropdown_reverse = block.getFieldValue('reverse'); // var dropdown_turn_left = block.getFieldValue('turn_left'); // var dropdown_turn_right = block.getFieldValue('turn_right'); // var dropdown_stop = block.getFieldValue('stop'); // var dropdown_speed_0 = block.getFieldValue('speed_0'); // var dropdown_speed_80 = block.getFieldValue('speed_80'); // var dropdown_speed_100 = block.getFieldValue('speed_100'); // var dropdown_speed_120 = block.getFieldValue('speed_120'); // var button_setup = '#define FW '+dropdown_run+'\n' + // '#define BW '+dropdown_reverse+'\n' + // '#define LT '+dropdown_turn_left+'\n' + // '#define RT '+dropdown_turn_right+'\n' + // '#define ST '+dropdown_stop+'\n' + // '#define SPD0 '+dropdown_speed_0+'\n' + // '#define SPD1 '+dropdown_speed_80+'\n' + // '#define SPD2 '+dropdown_speed_100+'\n' + // '#define SPD3 '+dropdown_speed_120+'\n'; // Blockly.Python.addDeclaration('button_setup', button_setup); // var ir_setup = 'int bufTime = 20;'; // Blockly.Python.addDeclaration('ir_setup', ir_setup); // var sendCode = 'void sendCode(void) {\n' + // ' if ( !gotNew ) { //We have already sent this so handle toggle bits\n' + // ' if (codeProtocol == RC5) {\n' + // ' codeValue ^= 0x0800;\n' + // ' }\n' + // ' else if (codeProtocol == RC6) {\n' + // ' switch (codeBits) {\n' + // ' case 20: codeValue ^= 0x10000; break;\n' + // ' case 24: codeValue ^= 0x100000; break;\n' + // ' case 28: codeValue ^= 0x1000000; break;\n' + // ' case 32: codeValue ^= 0x8000; break;\n' + // ' }\n' + // ' }\n' + // ' }\n' + // ' gotNew = false;\n' + // ' if (codeProtocol == UNKNOWN) {\n' + // ' //The raw time values start in decodeBuffer[1] because\n' + // ' //the [0] entry is the gap between frames. The address\n' + // ' //is passed to the raw send routine.\n' + // ' // codeValue = (uint32_t) & (recvGlobal.decodeBuffer[1]);\n' + // ' //This is not really number of bits. It is the number of entries\n' + // ' //in the buffer.\n' + // ' codeBits = recvGlobal.decodeLength - 1;\n' + // ' Serial.println(F("Sent raw"));\n' + // ' }\n' + // ' mySender.send(codeProtocol, codeValue, codeBits);\n' + // ' if (codeProtocol == UNKNOWN) return;\n' + // ' Serial.print(F("Sent "));\n' + // ' Serial.print(Pnames(codeProtocol));\n' + // ' Serial.print(F(" Value:0x"));\n' + // ' Serial.println(codeValue, HEX);\n' + // '}\n' + // '\n'; // Blockly.Python.addFunction('sendCode', sendCode); // var storeCode = 'void storeCode(void) {\n' + // ' gotNew = true; gotOne = true;\n' + // ' codeProtocol = myDecoder.protocolNum;\n' + // ' Serial.print(F("Received "));\n' + // ' Serial.print(Pnames(codeProtocol));\n' + // ' if (codeProtocol == UNKNOWN) {\n' + // ' Serial.println(F(" saving raw data."));\n' + // ' // myDecoder.dumpResults();\n' + // ' // codeValue = myDecoder.value;\n' + // ' }\n' + // ' else {\n' + // ' if (myDecoder.value == REPEAT_CODE) {\n' + // ' // Do not record a NEC repeat value as that is useless.\n' + // ' Serial.println(F("repeat; ignoring."));\n' + // ' } else {\n' + // ' codeValue = myDecoder.value;\n' + // ' codeBits = myDecoder.bits;\n' + // ' }\n' + // ' Serial.print(F(" Value:0x"));\n' + // ' Serial.println(codeValue, HEX);\n' + // ' }\n' + // '}\n' + // '\n'; // Blockly.Python.addFunction('storeCode', storeCode); // var speedProc = 'void speedProc() {\n' + // ' switch(myDecoder.value)\n' + // ' {\n' + // ' case SPD0:\n' + // ' stop();\n' + // ' delay(20);\n' + // ' carSpeed = 0;\n' + // ' break;\n' + // ' case SPD1:\n' + // ' stop();\n' + // ' delay(20);\n' + // ' carSpeed = 80;\n' + // ' break;\n' + // ' case SPD2:\n' + // ' stop();\n' + // ' delay(20);\n' + // ' carSpeed = 100;\n' + // ' break;\n' + // ' case SPD3:\n' + // ' stop();\n' + // ' delay(20);\n' + // ' carSpeed = 120;\n' + // ' break;\n' + // ' }\n' + // '}\n' + // '\n'; // Blockly.Python.addFunction('speedProc', speedProc); // var carProc = 'void carProc() {\n' + // ' switch (myDecoder.value) {\n' + // ' case FW:\n' + // ' stop();\n' + // ' delay(bufTime);\n' + // ' move(1, carSpeed*2, 0);\n' + // ' move(2, carSpeed*2, 0);\n' + // ' delay(bufTime);\n' + // ' digitalWrite(0, HIGH);\n' + // ' break;\n' + // ' case BW:\n' + // ' stop();\n' + // ' delay(bufTime);\n' + // ' move(1, carSpeed*2, 1);\n' + // ' move(2, carSpeed*2, 1);\n' + // ' delay(bufTime);\n' + // ' digitalWrite(0, LOW);\n' + // ' break;\n' + // ' case LT:\n' + // ' stop();\n' + // ' delay(bufTime);\n' + // ' move(1, carSpeed, 0);\n' + // ' move(2, carSpeed*2, 0);\n' + // ' delay(bufTime);\n' + // ' digitalWrite(1, HIGH);\n' + // ' break;\n' + // ' case RT:\n' + // ' stop();\n' + // ' delay(bufTime);\n' + // ' move(1, carSpeed*2, 0);\n' + // ' move(2, carSpeed, 0);\n' + // ' digitalWrite(1, LOW);\n' + // ' delay(bufTime);\n' + // ' break;\n' + // ' case ST:\n' + // ' stop();\n' + // ' delay(bufTime);\n' + // ' break;\n' + // ' }\n' + // '}\n'; // Blockly.Python.addFunction('carProc', carProc); // var stop = 'void stop() {\n' + // ' //enable standby\n' + // ' digitalWrite(STBY, LOW);\n' + // '}\n'; // Blockly.Python.addFunction('stop', stop); // var code = 'speedProc();\n' + // ' carProc();\n' + // ' \n' + // ' if (myReceiver.getResults()) {\n' + // ' myDecoder.decode();\n' + // ' storeCode();\n' + // ' myReceiver.enableIRIn(); // Re-enable receiver\n' + // ' }\n' + // ' delay(100);\n'; // return code; // } // Blockly.Python['motor_path_planning'] = function(block) { // var motor_IR_decl = '#include \n' + // '#include \n' + // '#include \n' + // '#include \n' + // '#include \n' + // '#include \n' + // '#include \n' + // '#include \n' + // '#include \n' + //We need this for IRsendRaw // '#include \n' + // 'IRdecode myDecoder;\n' + // 'IRsend mySender;\n' + // '#include \n' + // 'IRrecv myReceiver(A2);\n' + //pin number for the receiver // 'int carSpeedIR = 100;\n' + // 'int bufTimeIR = 50;\n'; // Blockly.Python.addDeclaration('motor_IR_decl', motor_IR_decl); // Blockly.Python.addDeclaration('line_tracking_init_B', 'uint8_t codeProtocol;\nuint32_t codeValue;\nuint8_t codeBits;\nbool gotOne, gotNew;\n'); // // Blockly.Python.addSetup('path_planning_setup', Blockly.Python.statementToCode(block, 'motor_motion')); // return Blockly.Python.statementToCode(block, 'motor_motion'); // }; // Blockly.Python['motor_turn_left'] = function(block) { // var text_delay = block.getFieldValue('delay'); // var code = "adjustLeft();\ndelay("+text_delay+");\n"; // return code; // }; // Blockly.Python['motor_turn_right'] = function(block) { // var text_delay = block.getFieldValue('delay'); // var code = "adjustRight();\ndelay("+text_delay+");\n"; // return code; // }; // Blockly.Python['motor_go_forward'] = function(block) { // var text_delay = block.getFieldValue('delay'); // var code = "goStraight();\ndelay("+text_delay+");\n"; // return code; // }; // Blockly.Python['motor_go_backward'] = function(block) { // var text_delay = block.getFieldValue('delay'); // var code = "reverse();\ndelay("+text_delay+");\n"; // return code; // }; // Blockly.Python['motor_line_tracking'] = function(block) { // var text_pin_0 = block.getFieldValue('pin_0'); // var text_pin_1 = block.getFieldValue('pin_1'); // var text_speed = block.getFieldValue('speed'); // var LT_decl = '#define ANALOG_SENSOR_PIN_A '+text_pin_0+'\n' + // '#define ANALOG_SENSOR_PIN_B '+text_pin_1+'\n'; // Blockly.Python.addDeclaration('LT_decl',LT_decl); // var line_tracking_carLineTracking = 'void carLineTracking() {\n' + // ' LightAnalogValue_A = analogRead(ANALOG_SENSOR_PIN_A); //Read the voltage from sensor\n' + // ' LightAnalogValue_B = analogRead(ANALOG_SENSOR_PIN_B);\n' + // ' \n' + // //' carSpeed = analogRead(A3);\n' + // ' carSpeed = '+text_speed+';\n'+ // ' speedLeft = carSpeed;\n' + // ' speedRight = carSpeed;\n' + // ' /* Serial.println(LightAnalogValue_A);\n' + // ' Serial.println(LightAnalogValue_B);\n' + // ' Serial.println("---"); */\n' + // ' \n' + // ' if ( LightAnalogValue_A < threshold && LightAnalogValue_B < threshold )\n' + // ' { \n' + // ' goStraight();\n' + // ' Serial.println("Straight");\n' + // ' Serial.println(LightAnalogValue_A);\n' + // ' Serial.println(LightAnalogValue_B);\n' + // ' //digitalWrite(3, HIGH);\n' + // ' //digitalWrite(6, HIGH);\n' + // ' Serial.println("---");\n' + // ' } else if ( LightAnalogValue_A > threshold && LightAnalogValue_B < threshold )\n' + // ' {\n' + // ' adjustLeft();\n' + // ' Serial.println("Left");\n' + // ' Serial.println(LightAnalogValue_A);\n' + // ' Serial.println(LightAnalogValue_B);\n' + // ' //digitalWrite(6, HIGH);\n' + // ' Serial.println("---");\n' + // ' } else if ( LightAnalogValue_A < threshold && LightAnalogValue_B > threshold )\n' + // ' {\n' + // ' adjustRight();\n' + // ' Serial.println("Right");\n' + // ' Serial.println(LightAnalogValue_A);\n' + // ' Serial.println(LightAnalogValue_B);\n' + // ' //digitalWrite(3, HIGH);\n' + // ' Serial.println("---");\n' + // ' } else\n' + // ' {\n' + // ' Stop();\n' + // ' Serial.println("Stopped");\n' + // ' Serial.println(LightAnalogValue_A);\n' + // ' Serial.println(LightAnalogValue_B);\n' + // ' Serial.println("---");\n' + // ' }\n' + // ' delay(10);\n' + // '}\n'; // Blockly.Python.addFunction('line_tracking_carLineTracking', line_tracking_carLineTracking); // var line_tracking_speedProc = 'void speedProc() {\n' + // ' switch(myDecoder.value)\n' + // ' {\n' + // ' case SPD0:\n' + // ' Stop();\n' + // ' delay(20);\n' + // ' carSpeedIR = 0;\n' + // ' break;\n' + // ' case SPD1:\n' + // ' Stop();\n' + // ' delay(20);\n' + // ' carSpeedIR = 80;\n' + // ' break;\n' + // ' case SPD2:\n' + // ' Stop();\n' + // ' delay(20);\n' + // ' carSpeedIR = 100;\n' + // ' break;\n' + // ' case SPD3:\n' + // ' Stop();\n' + // ' delay(20);\n' + // ' carSpeedIR = 120;\n' + // ' break;\n' + // ' }\n' + // '}\n'; // //Blockly.Python.addFunction('line_tracking_speedProc', line_tracking_speedProc); // var line_tracking_carProc = 'void carProc() {\n' + // ' switch (myDecoder.value) {\n' + // ' case FW:\n' + // ' Stop();\n' + // ' delay(bufTimeIR);\n' + // ' move(1, carSpeedIR*2, 0);\n' + // ' move(2, carSpeedIR*2, 0);\n' + // ' delay(bufTimeIR);\n' + // ' digitalWrite(0, HIGH);\n' + // ' break;\n' + // ' case BW:\n' + // ' Stop();\n' + // ' delay(bufTimeIR);\n' + // ' move(1, carSpeedIR*2, 1);\n' + // ' move(2, carSpeedIR*2, 1);\n' + // ' delay(bufTimeIR);\n' + // ' digitalWrite(0, LOW);\n' + // ' break;\n' + // ' case LT:\n' + // ' Stop();\n' + // ' delay(bufTimeIR);\n' + // ' move(1, carSpeedIR, 0);\n' + // ' move(2, carSpeedIR*2, 0);\n' + // ' delay(bufTimeIR);\n' + // ' digitalWrite(1, HIGH);\n' + // ' break;\n' + // ' case RT:\n' + // ' Stop();\n' + // ' delay(bufTimeIR);\n' + // ' move(1, carSpeedIR*2, 0);\n' + // ' move(2, carSpeedIR, 0);\n' + // ' digitalWrite(1, LOW);\n' + // ' delay(bufTimeIR);\n' + // ' break;\n' + // ' case ST:\n' + // ' Stop();\n' + // ' delay(bufTimeIR);\n' + // ' break;\n' + // ' }\n' + // '}\n'; // //Blockly.Python.addFunction('line_tracking_carProc', line_tracking_carProc); // var code = 'carLineTracking();\n'; // return code; // }; // Blockly.Python['motor_ultrasonic_pin'] = function(block) { // var text_pin_var0 = block.getFieldValue('pin_var0'); // var text_pin_0 = block.getFieldValue('pin_0'); // var text_pin_var1 = block.getFieldValue('pin_var1'); // var text_pin_1 = block.getFieldValue('pin_1'); // var text_pin_var2 = block.getFieldValue('pin_var2'); // var text_pin_2 = block.getFieldValue('pin_2'); // var text_pin_var3 = block.getFieldValue('pin_var3'); // var text_pin_3 = block.getFieldValue('pin_3'); // var text_pin_var4 = block.getFieldValue('pin_var4'); // var text_pin_4 = block.getFieldValue('pin_4'); // var text_pin_var5 = block.getFieldValue('pin_var5'); // var text_pin_5 = block.getFieldValue('pin_5'); // var text_max_distance_var = block.getFieldValue('max_distance_var'); // var text_max_distance = block.getFieldValue('max_distance'); // var text_real_time_distance_var = block.getFieldValue('real_time_distance_var'); // var text_real_time_distance = block.getFieldValue('real_time_distance'); // var motor_ultrasonic_decl = '#include \n' + // '#define TRIGGER_PIN 1\n' + // '#define ECHO_PIN 0\n' + // '#define ' + text_pin_var0 + ' '+text_pin_0+'\n' + // '#define ' + text_pin_var1 + ' '+text_pin_1+'\n' + // '#define ' + text_pin_var2 + ' '+text_pin_2+'\n' + // '#define ' + text_pin_var3 + ' '+text_pin_3+'\n' + // '#define ' + text_pin_var4 + ' '+text_pin_4+'\n' + // '#define ' + text_pin_var5 + ' '+text_pin_5+'\n' + // '#define ' + text_max_distance_var + ' ' + text_max_distance + '\n' + // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm. // '#define ' + text_real_time_distance_var + ' ' + text_real_time_distance + '\n' + // 'NewPing sonar(TRIGGER_PIN, ECHO_PIN, '+text_max_distance_var+');\n' + // 'int carSpeedSonar;\n' + // '#define POT A3\n' + // 'NewPing sonarHead('+text_pin_var0+', '+text_pin_var1 +', '+text_max_distance_var+');\n' + // 'NewPing sonarLeft('+text_pin_var2+', '+text_pin_var3+','+text_max_distance_var+');\n' + // 'NewPing sonarRigt('+text_pin_var4+', '+text_pin_var5+', '+text_max_distance_var+');\n'; // Blockly.Python.addDeclaration('motor_ultrasonic_decl', motor_ultrasonic_decl); // var ultrasonic_loop_code = 'delay(10); // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.\n' + // ' // Left: 1, 0\n' + // ' // Right: 0, 1\n' + // ' // Forward: 1, 1\n' + // ' int distHead = sonarHead.ping_cm();\n' + // ' int distLeft = sonarLeft.ping_cm();\n' + // ' int distRigt = sonarRigt.ping_cm();\n' + // ' // carSpeedSonar = analogRead(A3);\n' + // ' carSpeedSonar = 180;\n' + // ' move(0, carSpeedSonar - 20, 1);\n' + // ' move(1, carSpeedSonar - 20, 1);\n' + // ' if ( distHead < '+text_real_time_distance_var+' && distHead != 0 ) {\n' + // ' move(0, carSpeedSonar, 0);\n' + // ' move(1, carSpeedSonar, 0);\n' + // ' delay(100);\n' + // ' move(0, carSpeedSonar, 1);\n' + // ' move(1, carSpeedSonar, 0);\n' + // ' delay(100);\n' + // ' if ( distLeft > distRigt ) {\n' + // ' move(0, carSpeedSonar, 1);\n' + // ' move(1, carSpeedSonar, 0);\n' + // ' delay(50);\n' + // ' } else if (distLeft < distRigt) {\n' + // ' move(0, carSpeedSonar, 0);\n' + // ' move(1, carSpeedSonar, 1);\n' + // ' delay(150);\n' + // ' }\n' + // ' } else if ( distLeft < '+text_real_time_distance_var+' && distRigt < '+text_real_time_distance_var+' ) {\n' + // ' if ( distLeft > distRigt ) {\n' + // ' move(0, carSpeedSonar*1.2, 1);\n' + // ' move(1, carSpeedSonar, 0);\n' + // ' delay(80);\n' + // ' } else if (distLeft < distRigt) {\n' + // ' move(0, carSpeedSonar, 0);\n' + // ' move(1, carSpeedSonar*1.2, 1);\n' + // ' delay(80);\n' + // ' }\n' + // ' }\n' + // ' Serial.print("Left detect:\t");\n' + // ' Serial.print(sonarLeft.ping_cm()); // Send ping, get distance in cm and print result (0 = outside set distance range)\n' + // ' Serial.print("cm");\n' + // ' Serial.print("\t");\n' + // ' Serial.print("Head detect:\t");\n' + // ' Serial.print(sonarHead.ping_cm()); // Send ping, get distance in cm and print result (0 = outside set distance range)\n' + // ' Serial.print("cm");\n' + // ' Serial.print("\t");\n' + // ' Serial.print("Right detect:\t");\n' + // ' Serial.print(sonarRigt.ping_cm()); // Send ping, get distance in cm and print result (0 = outside set distance range)\n' + // ' Serial.println("cm");\n'; // return ultrasonic_loop_code; // }; // var count = 0; // Blockly.Python['motor_ultrasonic_custom_pin'] = function(block) { // var text_custom_pin_var0 = block.getFieldValue('custom_pin_var0'); // var text_custom_pin_0 = block.getFieldValue('custom_pin_0'); // var text_custom_pin_var1 = block.getFieldValue('custom_pin_var1'); // var text_custom_pin_1 = block.getFieldValue('custom_pin_1'); // var text_custom_max_distance_var = block.getFieldValue('custom_max_distance_var'); // var text_custom_max_distance = block.getFieldValue('custom_max_distance'); // var text_custom_real_time_distance_var = block.getFieldValue('custom_real_time_distance_var'); // var text_custom_real_time_distance = block.getFieldValue('custom_real_time_distance'); // var custom_decl = '#define ' + text_custom_pin_var0 + ' '+text_custom_pin_0+'\n' + // '#define ' + text_custom_pin_var1 + ' '+text_custom_pin_1+'\n' + // '// Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.\n' + // '#define ' + text_custom_max_distance_var + ' ' + text_custom_max_distance + '\n' + // '#define ' + text_custom_real_time_distance_var + ' '+text_custom_real_time_distance+'\n'; // Blockly.Python.addDeclaration('custom_decl' + count, custom_decl, false); // count += 1; // return ''; // }; // Blockly.Python['motor_controller'] = function(block) { // var text_motor_name = block.getFieldValue('motor_name'); // var number_motor_speed = block.getFieldValue('motor_speed'); // var dropdown_motor_direction = block.getFieldValue('motor_direction'); // // TODO: Assemble JavaScript into code variable. // var code = 'move('+text_motor_name+','+number_motor_speed+','+dropdown_motor_direction+');\n'; // return code; // }; // Blockly.Python['motor_stop'] = function(block) { // var code = "while(true) {\n" + // "Stop();\n" + // "}\n"; // return code; // }; // Blockly.Python['motor_pause'] = function(block) { // var text_delay = block.getFieldValue('delay'); // var code = "Stop();\ndelay("+text_delay+");\n"; // return code; // }; // Blockly.Python['robotcar3in1'] = function(block) { // var declarationUltra = '#include \n' + // '#define TRIGGER_PIN A6\n' + // '#define ECHO_PIN A7\n' + // '#define MAX_DISTANCE 50\n' + // 'NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);\n' + // 'int carSpeedSonar;\n' + // '#include \n' + // '#include \n' + // '#include \n' + // '#include \n' + // '#include \n' + // '#include \n' + // '#include \n' + // '#include \n' + // '#include \n' + // '#include \n' + // 'IRdecode myDecoder;\n' + // 'IRsend mySender;\n' + // '#include \n' + // 'IRrecv myReceiver(A2);\n' + // 'uint8_t codeProtocol;\n' + // 'uint32_t codeValue;\n' + // 'uint8_t codeBits;\n' + // 'bool gotOne, gotNew;\n' + // '#define FW 0xFF18E7\n' + // '#define BW 0xFF4AB5\n' + // '#define LT 0xFF10EF\n' + // '#define RT 0xFF5AA5\n' + // '#define ST 0xFF38C7\n' + // '#define SPD0 0xFF9867\n' + // '#define SPD1 0xFFA25D\n' + // '#define SPD2 0xFF629D\n' + // '#define SPD3 0xFFE21D\n' + // 'int carSpeedIR = 60;\n' + // 'int bufTimeIR = 50;\n' + // '#define ANALOG_SENSOR_PIN_A A4\n' + // '#define ANALOG_SENSOR_PIN_B A0\n' + // '#define SWITCHA 12\n' + // '#define SWITCHB 13\n' + // 'int LightAnalogValue_A, LightAnalogValue_B;\n' + // 'int PWMA = 5;\n' + // 'int AIN2 = 10;\n' + // 'int AIN1 = 9;\n' + // 'int PWMB = 6;\n' + // 'int BIN1 = 4;\n' + // 'int BIN2 = 8;\n' + // 'int RA = 0;\n' + // 'int RB = 0;\n' + // 'int STBY = A5;\n' + // 'volatile int state = LOW;\n' + // 'int threshold = 500;\n' + // 'int carSpeed, speedLeft, speedRight;\n'; // var setupUltra = 'pinMode(STBY, OUTPUT); pinMode(PWMA, OUTPUT); pinMode(AIN1, OUTPUT); pinMode(AIN2, OUTPUT); pinMode(PWMB, OUTPUT); pinMode(BIN1, OUTPUT); pinMode(BIN2, OUTPUT); pinMode(A3, INPUT); pinMode(SWITCHA, INPUT); pinMode(3, INPUT); gotOne = false; gotNew = false; codeProtocol = UNKNOWN; codeValue = 0; myReceiver.enableIRIn();'; // var functionUltra = 'void storeCode(void) { gotNew = true; gotOne = true; codeProtocol = myDecoder.protocolNum; Serial.print(F("Received ")); Serial.print(Pnames(codeProtocol)); if (codeProtocol == UNKNOWN) { Serial.println(F(" saving raw data.")); } else { if (myDecoder.value == REPEAT_CODE) { Serial.println(F("repeat; ignoring.")); } else { codeValue = myDecoder.value; codeBits = myDecoder.bits; } Serial.print(F(" Value:0x")); Serial.println(codeValue, HEX); }}void sendCode(void) { if ( !gotNew ) { if (codeProtocol == RC5) { codeValue ^= 0x0800; } else if (codeProtocol == RC6) { switch (codeBits) { case 20: codeValue ^= 0x10000; break; case 24: codeValue ^= 0x100000; break; case 28: codeValue ^= 0x1000000; break; case 32: codeValue ^= 0x8000; break; } } } gotNew = false; if (codeProtocol == UNKNOWN) { codeBits = recvGlobal.decodeLength - 1; Serial.println(F("Sent raw")); } mySender.send(codeProtocol, codeValue, codeBits); if (codeProtocol == UNKNOWN) return; Serial.print(F("Sent ")); Serial.print(Pnames(codeProtocol)); Serial.print(F(" Value:0x")); Serial.println(codeValue, HEX);}void adjustLeft() { move(1, speedLeft, 1); move(2, speedRight, 2); delay(100);}void adjustRight() { move(1, speedLeft, 2); move(2, speedRight, 1); delay(100);}void goStraight() { move(1, speedLeft, 2); move(2, speedRight, 2); delay(200);}void Stop() { move(1, 0, 2); move(2, 0, 2); delay(200);}void move(int motor, int speed, int direction){ digitalWrite(STBY, HIGH); boolean inPin1 = LOW; boolean inPin2 = HIGH; if (direction == 1) { inPin1 = HIGH; inPin2 = LOW; } if (motor == 1) { digitalWrite(AIN1, inPin1); digitalWrite(AIN2, inPin2); analogWrite(PWMA, speed); } else { digitalWrite(BIN1, inPin1); digitalWrite(BIN2, inPin2); analogWrite(PWMB, speed); }}void carLineTracking() { LightAnalogValue_A = analogRead(ANALOG_SENSOR_PIN_A); LightAnalogValue_B = analogRead(ANALOG_SENSOR_PIN_B); carSpeed = analogRead(A3); speedLeft = carSpeed/6; speedRight = carSpeed/6; if ( LightAnalogValue_A < threshold && LightAnalogValue_B < threshold ) { goStraight(); Serial.println("Straight"); Serial.println(LightAnalogValue_A); Serial.println(LightAnalogValue_B); Serial.println("---"); } else if ( LightAnalogValue_A > threshold && LightAnalogValue_B < threshold ) { adjustRight(); Serial.println("Left"); Serial.println(LightAnalogValue_A); Serial.println(LightAnalogValue_B); Serial.println("---"); } else if ( LightAnalogValue_A < threshold && LightAnalogValue_B > threshold ) { adjustLeft(); Serial.println("Right"); Serial.println(LightAnalogValue_A); Serial.println(LightAnalogValue_B); Serial.println("---"); } else { Stop(); Serial.println("Stopped"); Serial.println(LightAnalogValue_A); Serial.println(LightAnalogValue_B); Serial.println("---"); } delay(10);}void speedProc() { switch(myDecoder.value) { case SPD0: Stop(); delay(20); carSpeedIR = 0; break; case SPD1: Stop(); delay(20); carSpeedIR = 80; break; case SPD2: Stop(); delay(20); carSpeedIR = 100; break; case SPD3: Stop(); delay(20); carSpeedIR = 225; break; }}void carProc() { switch (myDecoder.value) { case FW: Stop(); delay(bufTimeIR); move(1, carSpeedIR*2, 0); move(2, carSpeedIR*2, 0); delay(bufTimeIR); digitalWrite(0, HIGH); break; case BW: Stop(); delay(bufTimeIR); move(1, carSpeedIR*2, 1); move(2, carSpeedIR*2, 1); delay(bufTimeIR); digitalWrite(0, LOW); break; case LT: Stop(); delay(bufTimeIR); move(1, carSpeedIR, 0); move(2, carSpeedIR*2, 0); delay(bufTimeIR); digitalWrite(1, HIGH); break; case RT: Stop(); delay(bufTimeIR); move(1, carSpeedIR*2, 0); move(2, carSpeedIR, 0); digitalWrite(1, LOW); delay(bufTimeIR); break; case ST: Stop(); delay(bufTimeIR); break; }}void carUltraSonar() { int dist = sonar.ping_cm(); int threshold = 20; carSpeedSonar = analogRead(A3); move(1, carSpeedSonar/5, 2); move(2, carSpeedSonar/5, 2); delay(50); if (dist < threshold && dist > 0) { move(1, carSpeedSonar/5, 0); move(2, carSpeedSonar/5, 1); delay(200); dist = 15; } Serial.print("Ping: "); Serial.print(sonar.ping_cm()); Serial.println("cm"); }'; // Blockly.Python.addDeclaration('declarationUltra', declarationUltra); // Blockly.Python.addSetup('setupUltra', setupUltra); // Blockly.Python.addFunction('functionUltra', functionUltra) // var code = 'int switchVal = digitalRead(SWITCHA); int switchValB = digitalRead(SWITCHB); int switchNum; if (switchValB == HIGH) { if (switchVal == HIGH) { switchNum = 0; } else if (switchVal == LOW) { switchNum = 1; } } else if (switchValB == LOW) { switchNum = 2; } switch (switchNum) { case 0: carLineTracking(); Stop(); delay(0); break; case 1: digitalWrite(3, HIGH); speedProc(); carProc(); if (myReceiver.getResults()) { myDecoder.decode(); storeCode(); myReceiver.enableIRIn(); } delay(100); break; case 2: delay(10); carUltraSonar(); Stop(); delay(0); break; } '; // return code; // };