readcurrents not working well

General discussion of using Roboclaw motor controllers
Post Reply
yacoda
Posts: 4
Joined: Tue May 14, 2019 7:06 am

readcurrents not working well

Post by yacoda » Tue May 14, 2019 8:21 am

hi, i'm trying to read currents from my motor and i'm getting [0,0].. this is the relevant part of my arduino code:


int16_t M1_current;
int16_t M2_current;

void loop() {

roboclaw.ReadCurrents(rb1,M1_current,M2_current);
Serial.print("M1 current=");
Serial.println(M1_current);
Serial.print("M2 current=");
Serial.println(M2_current);

downloaded the roboclaw.h today, and my roboclaw firmware is 4.1.33. please help me

User avatar
Basicmicro Support
Posts: 1201
Joined: Thu Feb 26, 2015 9:45 pm

Re: readcurrents not working well

Post by Basicmicro Support » Wed May 15, 2019 11:32 am

1. The included code is not sufficent to run your program. Please include a complete program that shows the problem.

2. You are not checking so see if the ReadCurrents commands completed successfully. You should always check the return value of the function to see if it is valid(true) or failed(false).

3. If your duty/power level is low enough you will get 0 current readings. This is because very low duty cycles are too small to read valid currents. The default blanking interval is 5.3%. Any duty below that will cause 0 current readings.

yacoda
Posts: 4
Joined: Tue May 14, 2019 7:06 am

Re: readcurrents not working well

Post by yacoda » Sun May 19, 2019 7:09 am

thank you for the respond

1. I'll write here the whole code.

2. I checked it with an "if", and saw that I was getting 0

3. I tried before printing to multiply the value by a hundred or a thousand and still got zero.

the whole code:

Code: Select all

#include "PPM.hpp"
#include <SoftwareSerial.h>
#include "RoboClaw.h"
#include <SD.h>
#include <SPI.h>

PPM Reciver;
int CS_PIN = 7;
File file;

#define rb1 0x80
#define rb2 0x82

SoftwareSerial serial(5,6);  
RoboClaw roboclaw(&serial,10000);

int channel_1, channel_2, channel_3,/*M1_current,M2_current,*/
channel_6, channel_8,channel_7, channel_5,
channel_4, upspeed,steerspeed,sprawlspeed,rampspeed;


int16_t M1_current;
int16_t M2_current;


void setup() {
  roboclaw.begin(38400);

  roboclaw.ForwardMixed(rb1, 0);
  roboclaw.TurnRightMixed(rb1, 0);
  roboclaw.ForwardM1(rb2, 0);
  roboclaw.ForwardM2(rb2, 0);
  Serial.begin(115200); // Serial for debug
  Reciver.begin(2, 1); // PPM on pin 2 triggering on HIGH state
}

void loop() {

  if(roboclaw.ReadCurrents(rb1,M1_current,M2_current)){
  Serial.print("M1 current=");
  //M1_current=M1_current*100;
  Serial.println((int16_t)M1_current);
  Serial.print("M2 current=");
  //M2_current=M2_current*100;
  Serial.println(M2_current,DEC);

  }
  
    Serial.print("channel 1: ");
    Serial.println((int)Reciver.getValue(1));
    Serial.print("channel 2: ");
    Serial.println((int)Reciver.getValue(2));
    Serial.print("channel 3: ");
    Serial.println((int)Reciver.getValue(3));
    Serial.print("channel 4: ");
    Serial.println((int)Reciver.getValue(4));
    Serial.print("channel 5: ");
    Serial.println((int)Reciver.getValue(5));
    Serial.print("channel 6: ");
    Serial.println((int)Reciver.getValue(6));
    Serial.print("channel 7: ");
    Serial.println((int)Reciver.getValue(7));
    Serial.print("channel 8: ");
    Serial.println((int)Reciver.getValue(8));
    Serial.print("upspeed= ");
    Serial.println((int)upspeed);
    Serial.print("steerspeed= ");
    Serial.println((int)steerspeed);
    Serial.print("sprawlspeed= ");
    Serial.println((int)sprawlspeed);
    Serial.print("rampspeed= ");
    Serial.println((int)rampspeed);
    //Serial.print("Motor One current= ");
  ///  Serial.println(currentFromMotorOne);
    delay(10);
  channel_1 = Reciver.getValue(1); //READ PULSE WIDTH IN M.SEC (NUMBER BETWEEN 997-1997)-channel 1
  channel_2 = Reciver.getValue(2); //READ PULSE WIDTH IN M.SEC (NUMBER BETWEEN 997-1997)-channel 2
  channel_3 = Reciver.getValue(3); //READ PULSE WIDTH IN M.SEC (NUMBER BETWEEN 997-1997)-channel 3
  channel_4 = Reciver.getValue(4); //READ PULSE WIDTH IN M.SEC (NUMBER BETWEEN 997-1997)-channel 4
  channel_5 = Reciver.getValue(5); //READ PULSE WIDTH IN M.SEC (NUMBER BETWEEN 997-1997)-channel 5
  channel_6 = Reciver.getValue(6); //READ PULSE WIDTH IN M.SEC (NUMBER BETWEEN 997-1997)-channel 6
  channel_7 = Reciver.getValue(7); //READ PULSE WIDTH IN M.SEC (NUMBER BETWEEN 997-1997)-channel 7
  channel_8 = Reciver.getValue(8); //READ PULSE WIDTH IN M.SEC (NUMBER BETWEEN 997-1997)-channel 8
 
  delay(10);

//code code code code code code code code code code code code 

upspeed=0;
steerspeed=0;
sprawlspeed=0;
rampspeed=0;

if (channel_7>1500)///A is down= all of
{
roboclaw.ForwardMixed(rb1, 0);
roboclaw.TurnRightMixed(rb1, 0);
roboclaw.ForwardM1(rb2, 0);
roboclaw.ForwardM2(rb2, 0);
}



if (channel_7<1500)///A is up= normal mode
{


if (channel_2 < 1490)  //up up up up up up up up up 
{
upspeed=map(channel_2,1490,995,0,127);
if (upspeed>120){
{
  upspeed=127;
}
 if (upspeed<3){
  upspeed=0;}
}
  roboclaw.ForwardMixed(rb1, upspeed);
}


if (channel_2 > 1505)
{
upspeed=map(channel_2,995,1490,127,0);
upspeed=upspeed*-1;
if (upspeed>58){
  upspeed=127;}
  if (upspeed<3){
  upspeed=0;}
roboclaw.BackwardMixed(rb1, upspeed);
}
   if (channel_2 >= 1490 && channel_2 <= 1505 )  
{
  roboclaw.BackwardMixed(rb1, 0);
}


  if (channel_1 < 1495)  //steer steer steer steer steer steer steer steer steer steer steer steer steer steer 
{
steerspeed=map(channel_1,1495,1027,0,127);
if (steerspeed>120)
{
  steerspeed=127;
}
 if (steerspeed<7){
  steerspeed=0;}
roboclaw.TurnRightMixed(rb1, steerspeed);
}
  if (channel_1 <= 1515 && channel_1 >= 1495){
roboclaw.TurnRightMixed(rb1, 0);
}
if (channel_1 > 1515)
{
steerspeed=map(channel_1,1515,1997,0,127);

if (steerspeed>120){
  steerspeed=127;}
   if (steerspeed<7){
  steerspeed=0;}
roboclaw.TurnLeftMixed(rb1, steerspeed);
}
  if (channel_1 <= 1515 && channel_1 >= 1495){
roboclaw.TurnRightMixed(rb1, 0);}

if (channel_3 < 1530)  //sprawl sprawl sprawl sprawl sprawl sprawl sprawl sprawl sprawl sprawl sprawl sprawl sprawl sprawl 
{
sprawlspeed=map(channel_3,1535,1005,0,127);
if (sprawlspeed>120)
{
  sprawlspeed=127;
}
 if (sprawlspeed<3){
  sprawlspeed=0;}
    if ((channel_3 >=1530 && channel_3 <= 1550) || channel_3 < 950) {
  sprawlspeed=0;}
roboclaw.ForwardM1(rb2, sprawlspeed);
}
if (channel_3 > 1550)
{
sprawlspeed=map(channel_3,1550,1990,0,127);
//sprawlspeed=sprawlspeed*-1;
if (sprawlspeed>120){
  sprawlspeed=127;}
  if (sprawlspeed<3){
  sprawlspeed=0;}
  if ((channel_3 >=1530 && channel_3 <= 1550) || channel_3 < 950) {
  sprawlspeed=0;}
roboclaw.BackwardM1(rb2, sprawlspeed);
}

if (channel_5 < 1300)  //ramp ramp ramp ramp ramp ramp ramp ramp ramp ramp ramp ramp ramp ramp ramp ramp 
{
roboclaw.ForwardM2(rb2, 25);//ramp up
rampspeed=25;
}
if (channel_5 > 1600)
{
roboclaw.BackwardM2(rb2, 15);//ramp down
rampspeed=-15;
}
if (channel_5 < 1600 && channel_5 > 1300)
{
roboclaw.BackwardM2(rb2, 0);
rampspeed=0;
}
}
}

User avatar
Basicmicro Support
Posts: 1201
Joined: Thu Feb 26, 2015 9:45 pm

Re: readcurrents not working well

Post by Basicmicro Support » Mon May 20, 2019 10:07 am

When you post code please us the code brackets. Just select the code and click the (</>) button to add them. I've added them to your post already but in the future please use them.

Please submit the simplest program you can that reproduces your problem.

Also what is PPM.hpp? Is that code to read an RC PPM signal? If so it is almost certainly using an interrupt. If that is likely you wont be able to use software serial to communicate with the Roboclaw. Sent commands will function but you wont be able to read back data accurately. Software serial relies on an on-change interrupt to detect the start bit. Other interrupts will interfere with that causing invalid data to be received.

Thanks.

yacoda
Posts: 4
Joined: Tue May 14, 2019 7:06 am

Re: readcurrents not working well

Post by yacoda » Tue May 21, 2019 7:02 am

so this is a simpler code (still ReadCurrent not working):

Code: Select all


#include <SoftwareSerial.h>
#include "RoboClaw.h"

#define rb1 0x80
#define rb2 0x82

SoftwareSerial serial(5,6);  
RoboClaw roboclaw(&serial,10000);

int16_t M1_current;
int16_t M2_current;

void setup() {
  roboclaw.begin(38400);

  roboclaw.ForwardMixed(rb1, 0);
  roboclaw.TurnRightMixed(rb1, 0);
  roboclaw.ForwardM1(rb2, 0);
  roboclaw.ForwardM2(rb2, 0);
  Serial.begin(115200); // Serial for debug
}

void loop() {


  roboclaw.ForwardMixed(rb1, 127);
   
  if(roboclaw.ReadCurrents(rb1,M1_current,M2_current)){
  Serial.print("M1 current=");
  //M1_current=M1_current*100;
  Serial.println((int16_t)M1_current);
  Serial.print("M2 current=");
  //M2_current=M2_current*100;
  Serial.println(M2_current);

  }
}

the ppm you saw is from a remote receiver to the arduino, not for communication with the Roboclaw

User avatar
Basicmicro Support
Posts: 1201
Joined: Thu Feb 26, 2015 9:45 pm

Re: readcurrents not working well

Post by Basicmicro Support » Tue May 21, 2019 10:30 am

Did you understand what I meant about the PPM. I assumed it was for a remote receiver. It most likely uses interrupts. It WILL affect software serial communications. It may not be the cause of your ReadCurrents problem but it will effect software serial randomly. You will most likely need to use hardware serial with Roboclaw if you use PPM.

Just to be clear, can you see the current readings in Motion Studio?

If your ReadCurrents command has a return value of 0 the command is failing. Test that you can use ReadVersion.

Your code shows you are using 2 Roboclaws. If your wiring is wrong or the units are not setup for Multi-Unit mode you will not be able to read anything.

Please describe your wiring. Make sure you have set the Multi-Unit mode option in Motion Studio. Also there is an article on the main website that shows how to use Multiple units on a single serial port.

yacoda
Posts: 4
Joined: Tue May 14, 2019 7:06 am

Re: readcurrents not working well

Post by yacoda » Wed May 22, 2019 2:56 am

yes, i can see the current readings in Motion Studio.

both Roboclaws are setup for multi unit.

My wiring is as described in page 63 in the manual.
http://downloads.ionmc.com/docs/robocla ... manual.pdf

User avatar
Basicmicro Support
Posts: 1201
Joined: Thu Feb 26, 2015 9:45 pm

Re: readcurrents not working well

Post by Basicmicro Support » Wed May 22, 2019 10:08 am

You need to test your connection. Use the ReadVersion example. If you cant read the version you don't have it wired correctly.

Test a single unit NOT in multi-unit mode. Does that work? If yes your problem is in your multi-unit setup.

Assuming that is the case provide a picture of your multi-unit wiring. Make sure it shows everything.

Post Reply