Ranger motors work fine in mBlock but not using Arduino IDE


#1

I am having the exact same problem as this forum post: Mbot Ranger Motors work in mblock but not Arduino IDE environment

For example, I take a small generated program in mBlock that turns on the ring LED for 1 second and then runs the motors for 5 seconds (see below).

It works perfectly fine if I compile and run with mBlock (that is, I copy the Arduino code it generates and then copy it to the mBlock Arduino tab and compile/upload).

But if I copy this to the Arduino IDE the lights work but not the motors.

Before you ask let me answer some questions:

  1. Yes I am using the Github library from this link: https://github.com/Makeblock-official/Makeblock-Libraries

  2. Yes the power is on. If I compile and upload with the mBlock program motors work perfect.

  3. Yes the batteries are fine

    // generated by mBlock5 for mBot Ranger
    // codes make you happy

    #include <Arduino.h>
    #include <Wire.h>
    #include <SoftwareSerial.h>
    #include <MeAuriga.h>

    MeRGBLed rgbled_0(0, 12);
    MeEncoderOnBoard Encoder_1(SLOT1);
    MeEncoderOnBoard Encoder_2(SLOT2);
    MeLightSensor lightsensor_12(12);

    void isr_process_encoder1(void)
    {
    if(digitalRead(Encoder_1.getPortB()) == 0){
    Encoder_1.pulsePosMinus();
    }else{
    Encoder_1.pulsePosPlus();
    }
    }
    void isr_process_encoder2(void)
    {
    if(digitalRead(Encoder_2.getPortB()) == 0){
    Encoder_2.pulsePosMinus();
    }else{
    Encoder_2.pulsePosPlus();
    }
    }
    void move(int direction, int speed)
    {
    int leftSpeed = 0;
    int rightSpeed = 0;
    if(direction == 1){
    leftSpeed = -speed;
    rightSpeed = speed;
    }else if(direction == 2){
    leftSpeed = speed;
    rightSpeed = -speed;
    }else if(direction == 3){
    leftSpeed = -speed;
    rightSpeed = -speed;
    }else if(direction == 4){
    leftSpeed = speed;
    rightSpeed = speed;
    }
    Encoder_1.setTarPWM(leftSpeed);
    Encoder_2.setTarPWM(rightSpeed);
    }

    void _delay(float seconds) {
    if(seconds < 0.0){
    seconds = 0.0;
    }
    long endTime = millis() + seconds * 1000;
    while(millis() < endTime) _loop();
    }

    void setup() {
    rgbled_0.setpin(44);
    rgbled_0.fillPixelsBak(0, 2, 1);
    TCCR1A = _BV(WGM10);
    TCCR1B = _BV(CS11) | _BV(WGM12);
    TCCR2A = _BV(WGM21) | _BV(WGM20);
    TCCR2B = _BV(CS21);
    attachInterrupt(Encoder_1.getIntNum(), isr_process_encoder1, RISING);
    attachInterrupt(Encoder_2.getIntNum(), isr_process_encoder2, RISING);
    randomSeed((unsigned long)(lightsensor_12.read() * 123456));

    rgbled_0.setColor(0,255,0,0);
    rgbled_0.show();
    _delay(1);
    rgbled_0.setColor(0,0,0,0);
    rgbled_0.show();

    move(1, 50 / 100.0 * 255);
    _delay(5);
    move(1, 0);

    }

    void _loop() {
    Encoder_1.loop();
    Encoder_2.loop();
    }

    void loop() {
    _loop();
    }


#2

I couldn’t see anything logically wrong with the code, so just uploaded and ran your code on my Ranger, and all is good.

When you say ‘the power is on’, is the green LED beside port 1 on? If that is not on, then you have logic (i.e. can run LEDs), but you don’t have power to the motors.

Can’t think what else to suggest, but try uploading and running your code un-tethered to USB, that way the power HAS to be on for anything to operate.


#3

AurigaPowerLED

I’ve also put together a coding reference for ranger here: https://docs.google.com/document/d/1EpMWJo9pP2J_pstzXA-XHK8t00Z70SCZYwZ_Kl7VLuw/edit with a number of examples you could play around with.


#4

Hi Elliot,

Thank you for the quick response.

We have the same issue. I am trying to find a lower end platform which we can use for middle school students. We have more 270 students at our center. Ability to run only using Arduino IDE will be a determining factor whether we can migrate to this ranger bot.

Thus, the solution to this issue will determine whether we will have our hundreds of students to invest in this Ranger platform in the coming Fall.

Unfortunately, your first statement did not address the main issue, but repeating what they said.

  1. From the multiple posts, the posters clearly showed it is not a logical error in the code.

  2. "uploaded and ran your code on my Ranger. " Of course we have done that. Otherwise, we could not have found the issue.

The foremost trouble shooting question is:

  1. did you yourself have the same set of code running independently using Arduino IDE? As the issue “clearly” has been stated - It did work in the Block IDE, not the Arduino IDE. Did you try that yourself? ONLY using Arduino IDE, not the mBlock IDE. Did your ranger bot actually moving the motors when it started up “Using Arduino IDE”?

  2. “running your code un-tethered to USB”. Are you suggesting that this only work un-tethered with USB?!

“… that way the power HAS to be on for anything to operate.” Elliot, thank you for that common sense suggestion. We have been working with robotics for more than 15 years. We have worked with also embedded system and various controllers. I can ensure we have such BASIC common sense knowledge. So, to ease your doubt. Yes, we know that motors need power tor run.

The immediate question to trouble shoot this issue is :

  • what could have contributed the difference between the library being used in the mBlock IDE vs the Arduino IDE. This is where we need to focus on.

#5

Did you use the mBlock IDE to upload or the Arduino IDE to upload? That is the only difference.

This isn’t an issue of the power being on or off because I can upload with the Arduino IDE and no motors run but if I immediately upload with the mBlock IDE the motors run fine.


#6

Yes, yes and yes. I thought it was fairly obvious as the mBlock code wasn’t shared, only the IDE, but I hope that makes it clearer.

No. See your following comment, it was merely a diagnostics step. I have no knowledge of your skills or experience, therefore was trying to eliminate possibilities. One thing I have learned in debugging code over my 30 years experience is the only way to diagnose a problem is to take nothing for granted.

You may want to look again at the libraries and configuration of your Arduino IDE, and the document I shared gives some information on that, but it seems you have plenty of diagnostic experience so I will step back leave you to the Makeblock support to see if they can assist.

Good luck!


#7

It will be a problem in the setup of the Ardunio IDE.

Mblock uses the Ardunio compiler to compile and flash the program to the ranger.

First of all does all other code work correctly from Ardunio IDE?? is it only the motor code that doesn’t??


#8

Hi dennisma & e.cheung,

This issue does happen using the arduino 1.8.10 which is the version integrated with the mBlock 5 software.
While if I test the codes with arduino 1.6.5, it works properly. I have reported this issue to our r&d guys and will updates here.


#9

I’m using Arduino 1.8.10 with no issues with the 1.8.2 boards library

arduino1810


#10

Yes thank you it does appear to work with 1.6.5 just fine. That will be ok for us then.


#11

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.