Megapi programming - encoder motor wont stop


#1

Hi,

It seems I am having some problems while trying the NodeJS API on Ultimate 2.0 kit. I am using Raspberry Pi and connected over USB. Before connecting I have updated the MegaPI firmware using mBlock desktop app. From Makeblock iPhone app it says that the firmware version “0e.01.014” us ip to date. Now, after following instructions of NodeJS MegaPi programming, I face two problems

  1. The encoder motor does not stop, no matter which function (encoderMotorMove, encoderMotorMoveTo, encoderMotorRun) I use. Is there a bug in the firmware or do I need to add anything more in the code?

Here is a simple code

var MegaPi = require('megapi').MegaPi;
var bot = new MegaPi("/dev/ttyUSB0",onStart);

function onStart(){
        setTimeout(run,1000);
}
function run() {
        bot.encoderMotorMoveTo(1,-10,1,bye);
        bot.encoderMotorMoveTo(2,10,1,bye);
}
function bye(){
        console.log("Bye");
}

Here, I have tried the “position” parameter of encoderMotorMoveTo function to be 1000, 100, 10 or even 1, but nothing seems to stop it from running forever.

  1. I want to understand why we use setTimeout in onStart() function
    It seems the code wont run if I use timeout value of anything less than 900 (even though the example codes use 500).
    I want to create NodeJS web interface with html buttons, so that different motors will run based on button press events. So do I need to add timeout for every control or is it just for the beginning?

#2

I see the same issue happened to someone else with Python API too, here Does MegaPi can connect Raspberry Pi with USB cable?


#3

I removed the reference of Node.JS from the topic title as I see the same issue with Python. The motors never stop. So basically “encoderMotorRun”, “encoderMotorMove” and “encoderMotorMoveTo” are all same, rather none of these functions even honor the speed parameter passed, it only behaves like any non-zero speed means run with 100% and zero speed means stop!

Here is my Python code and no finish callback is ever called because the encoderMotorMoveTo keeps on running forever

from megapi import *
def onForwardFinish(slot):
        sleep(0.4);
        bot.encoderMotorMoveTo(slot,100,-1000,onBackwardFinish);
def onBackwardFinish(slot):
        sleep(0.4);
        bot.encoderMotorMoveTo(slot,100,1000,onForwardFinish);
if __name__ == '__main__':
        bot = MegaPi()
        bot.start('/dev/ttyUSB0')
        bot.encoderMotorRun(1,0);
        sleep(1);
        onForwardFinish(1);
        while 1:
                continue;

#4

I had similar problem (motors didn’t stop). My solution was to clear the EEPROM. Probably I had somehow set a wrong bluetooth mode (which caused extra code to run on each loop iteration, beside the commands coming from the serial).

See here for more info: Encoder motors keep running on MegaPi port 1 and port 2


#5