Things used in this project

Code

Kittenbot_ext.jsJavaScript
Extension for Scratch2
// kittenbot_ext.js
(function(ext){

    var connected = false;
    var notifyConnection = false;
    var device = null;
    var flag = 0;
    var serialPort = require('serialport');
    var SerialPort = serialPort;
    var motorMap = {'M1':0, 'M2':1, 'M3':2, 'M4':3};
    var sp = new SerialPort("/dev/ttyS0",{
      baudrate:115200,
      databits: 8,
      parity: 'none',
      stopBits: 1,
      flowControl: false
    })
    
    sp.on('open',function(){
      console.log("port open");
      sp.write("M0\r\n");
    })

    sp.on('data', function(data){
      var rx = data.toString('utf8');
      console.log('>> ',rx);
      if(rx.indexOf('M0')>-1){
        connected = true;
      }
      if(rx.indexOf('M3')>-1){
        var rxArray=rx.split(" ");
        flag = rxArray[rxArray.indexOf('M3')+2];
      }
      if(rx.indexOf('M5')>-1){
        var rxArray=rx.split(" ");
        flag = rxArray[rxArray.indexOf('M5')+2];
      }
    })

    ext._getStatus = function() {
        if (!connected){
            console.log("M0\r\n"); // check if host app online
            sp.write("M0\r\n");
            return { status:1, msg:'Disconnected' };
        }else{
            return { status:2, msg:'Connected' };
        }
    };

    ext._deviceRemoved = function(dev) {
        console.log('Device removed');
        // Not currently implemented with serial devices
    };

    var potentialDevices = [];
    ext._deviceConnected = function(dev) {
        console.log("Device Connected "+dev.id);
    };

    ext._shutdown = function() {
        // TODO: Bring all pins down
        if (device) device.close();
        if (poller) clearInterval(poller);
        device = null;
    };

    ext.runMotor =  function(motor, speed){
        console.log("run motor "+motor+" "+speed);
        sendMsg("M201 "+motorMap[motor]+' '+speed+"\r\n");
    };

	
    ext.stop =  function(){
        console.log("motor stop");
        sendMsg("M102"+"\r\n");
    };

    ext.setPinmode =function(pin,pinSet){
       if(pinSet=="input") 
       sendMsg("M1 "+pin+' '+'0'+"\r\n");
       if(pinSet=="output")
       sendMsg("M1 "+pin+' '+'1'+"\r\n");
    };

    ext.digitalwritePin =function(pin,pinMode){
       if(pinMode=="high")
        sendMsg("M2 "+pin+' '+'1'+"\r\n");
       if(pinMode=="low")
        sendMsg("M2 "+pin+' '+'0'+"\r\n");
    };
    ext.analogwritePin =function(analogPin,pin){
        sendMsg("M4 "+analogPin+' '+pin+"\r\n");
    };
    ext.digitalRead =function(pin){
       sendMsg("M13 "+pin+ " "+'1'+"\r\n");
       return(flag);
    };
    ext.analogRead =function(Apin){
       sendMsg("M13 "+Apin+ " "+'2'+"\r\n");
       return(flag);
    };

    function sendMsg(msg){
        sp.write(msg);
        console.log("tx: "+msg);
    }

    // Block and block menu descriptions
    var descriptor = {
        blocks: [
            [' ', 'Stop', 'stop'],
            [' ', 'Run Motor %d.motorIdx at speed %d.motorPWM','runMotor', "M1", 100],
            [' ','pin Mode %n %d.pinSet','setPinmode',13,"input"],//设置pin状态
            [' ','digital write %n %d.pinMode','digitalwritePin',13,"high"],
            [' ','analog write %d.analogPin %n','analogwritePin',"13",100 ],
            ['r','digital read %n','digitalRead',11],
            ['r','analog read %d.Apin','analogRead',"A0"],
            ],
        menus: {
            onoff: ['ON', 'OFF'],
            led:['ALL','A','B','C','D'],
            motorIdx:["M1","M2","M3","M4","ALL"],
            motorPWM:[-255,-160,-100,0,100,160,255],
            pinSet:["input","output"],
            pinMode:["high","low"],
            analogPin:["13","10","9","5","3"],
            Apin:["A0","A1","A2","A3"],
        },
        url: 'http://kittenbot.cc/'
    };

    // Register the extension
    ScratchExtensions.register('kittenbot', descriptor, ext);

})({});

Credits

10150693 665457293520191 9013448938824302383 n nehurjmmcl
Rufus Zeng

Computer Engineering student at Binghamton University

Contact

Replications

Did you replicate this project? Share it!

I made one

Love this project? Think it could be improved? Tell us what you think!

Give feedback

Comments

Similar projects you might like

Wireless Programming and Debugging with STM32 and RPi
Intermediate
  • 25
  • 2

Full instructions

Are you tired of connecting programmer every time you want to change some functionality of your robot? Why not to try wireless programming?

Thunderboard Sense and RPi
Intermediate
  • 91
  • 2

Collecting rich sensor data over BLE and uploading it to the cloud for analysis.

LoRaWAN for Raspberry Pi with Worldwide Frequency Support
Intermediate
  • 3,568
  • 18

Full instructions

LoRaWAN LMIC 1.6 for Raspberry Pi with Dragino LoRA/GPS HAT or standalone RFM95W LoRa Module.

Human-Following Robot with Kinect
Intermediate
  • 2,371
  • 24

Full instructions

Instead of using single camera and complicated image recognition algorithms we can take advantage of Kinect libraries.

Automated Indoor Gardener
Intermediate
  • 3,869
  • 38

Full instructions

Never worry about dead plants again. This automated gardener never forgets to water your plants and provide artificial sunlight.

Computerception
Intermediate
  • 27,334
  • 20

Full instructions

Why not put a Raspberry Pi into a 2007 netbook?

Sign up / LoginProjectsPlatformsTopicsContestsLiveAppsBetaFree StoreBlog