Rufus ZengWuJiahaoRivenChristina Zhang
Published © MIT

Scratch 2.0 KittenBot Extension for Raspberry Pi 3 B+

In this project we demonstrate how to create a Scratch2.0 KittenBot extension for Raspberry Pi 3 to control external hardware.

IntermediateFull instructions provided2 hours1,103
 Scratch 2.0 KittenBot Extension for Raspberry Pi 3 B+

Things used in this project

Story

Read more

Code

Kittenbot_ext.js

JavaScript
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

Rufus Zeng

Rufus Zeng

2 projects • 12 followers
Computer Engineering student at Binghamton University
Contact
WuJiahao

WuJiahao

0 projects • 1 follower
Contact
Riven

Riven

2 projects • 12 followers
Contact
Christina Zhang

Christina Zhang

2 projects • 13 followers
Contact

Comments