EVShield motor control

I am trying to use an NXT Motor with EVShield to steer my robot car.
To do so I need to have the motor go to a specific rotation consistently.
My code is this:

in setup()
center_pos = evshield.bank_a.motorGetEncoderPosition(SH_Motor_2);

global functions:
void turn_left()
{
int32_t newPos = center_pos - 30;
evshield.bank_a.motorRunTachometer(SH_Motor_2, SH_Direction_Forward, 70, newPos, SH_Move_Absolute, SH_Completion_Dont_Wait, SH_Next_Action_Float);
}

void turn_right()
{
int32_t newPos = center_pos + 30;
evshield.bank_a.motorRunTachometer(SH_Motor_2, SH_Direction_Forward, 70, newPos, SH_Move_Absolute, SH_Completion_Dont_Wait, SH_Next_Action_Float);
}

void go_straight()
{
int32_t newPos = center_pos;
evshield.bank_a.motorRunTachometer(SH_Motor_2, SH_Direction_Forward, 70, newPos, SH_Move_Absolute, SH_Completion_Dont_Wait, SH_Next_Action_Float);
}

So, in loop() I am using a timer to call these functions after a few seconds:

turn_left();

// some time later

go_straight();

// later

turn_right();

// later

go_straight();

What I am finding is that the motor seems to almost turns a random amount. It does the first turn left fine, but the go straight call seems to wayyyy over rotate the motor. Then turn right does not go to the proper spot.

Is there something else I should be doing? Is this a driver bug?
I don't see the driver for EVShield source code anywhere, otherwise I would try tweaking it. Is it open source?

Thanks for any help that can be provided.

One thing I tried was to set the motors to max speed and use next action brake. That seems to work much better.
Slows speeds seem flakey.

Hi, @sisraelson
Welcome to the forum.

Please read the post at the start of any forum , entitled "How to use this Forum".

Can you post your code as per the link?
Can you please post links to data/specs for NXT Motor and EXShield.

We need to see a circuit diagram, including your power supplies.

Thanks.. Tom... :grinning: :+1: :coffee: :australia:

Ahh, good points - the problem might be outside of what I am specifically doing.
Here goes:
Hardware: Arduino UNO VID: 0x2341 PID: 0x0043
With EVShield-v2 by mindsensors.com.
6 AA rechargeable battery pack.

3 Lego NXT Large motors connected to ports A-M1, A-M2, B-M1.

I am using the EVShield library from here: GitHub - mindsensors/EVShield: EVShield Arduino library

My code is this:

#include <Wire.h>
#include <EVShield.h>
#include "math.h"

EVShield          evshield(0x34,0x36);
int32_t center_pos;
int carState = 0;
int timerState = 0;
unsigned long timer = 0;

void changeCarStateAfterTime(int newState, int delay)
{
  timer = millis() + delay;
  timerState = newState;
}

void setup()
{
    Serial.begin(115200);       // start serial for output
    delay(1000);                // wait two seconds, allowing time to
                                // activate the serial monitor

    evshield.init( SH_HardwareI2C );

    evshield.ledSetRGB(0,25,0);
    evshield.bank_a.motorReset();
    evshield.bank_b.motorReset();

    while (!evshield.getButtonState(BTN_GO)) {
        if (millis() % 1000 < 6) {
            Serial.println("Press GO button to continue");
        }
    }
  changeCarStateAfterTime(0, 1000);

  center_pos = evshield.bank_a.motorGetEncoderPosition(SH_Motor_2);
}

void turn_left()
{
  int32_t newPos = center_pos + 50;
  evshield.bank_a.motorRunTachometer(SH_Motor_2, SH_Direction_Forward, 100, newPos, SH_Move_Absolute, SH_Completion_Dont_Wait, SH_Next_Action_BrakeHold);
}

void turn_right()
{
  int32_t newPos = center_pos - 50;
  evshield.bank_a.motorRunTachometer(SH_Motor_2, SH_Direction_Forward, 100, newPos, SH_Move_Absolute, SH_Completion_Dont_Wait, SH_Next_Action_BrakeHold);
}

void go_straight()
{
  int32_t newPos = center_pos;
  evshield.bank_a.motorRunTachometer(SH_Motor_2, SH_Direction_Forward, 100, newPos, SH_Move_Absolute, SH_Completion_Dont_Wait, SH_Next_Action_BrakeHold);
}

void checkCarTimer(void)
{
  if (timer && millis() >= timer) {
    timer = 0;
    changeCarState(timerState);
  }
}

void goForwardInches(int inches)
{
  int degrees = 19 * inches;
  evshield.bank_a.motorRunDegrees(SH_Motor_1, SH_Direction_Forward, 30, degrees, SH_Completion_Dont_Wait, SH_Next_Action_BrakeHold);
  evshield.bank_b.motorRunDegrees(SH_Motor_1, SH_Direction_Forward, 30, degrees, SH_Completion_Dont_Wait, SH_Next_Action_BrakeHold);
}

// Two rotations = 37.5", therefore 1 degree = 0.05208". 19 degrees = 1"
void changeCarState(int newState)
{
  carState = newState;
  switch (carState)
  {
    case 0:
      go_straight();
      goForwardInches(88);  
      changeCarStateAfterTime(1, 8000);
    break;   
    case 1:
      turn_left();      
      changeCarStateAfterTime(2, 1000);
    break;
    case 2:
      goForwardInches(20);  
      changeCarStateAfterTime(3, 3000);
    break;
    case 3:
      go_straight();
      changeCarStateAfterTime(4, 1000);
    break;
    case 4:
      goForwardInches(30);  
      changeCarStateAfterTime(99, 5000);
    break;
    case 5:
      go_straight();
      changeCarStateAfterTime(5, 2000);
    break;
  default:
      evshield.bank_a.motorReset();
      evshield.bank_b.motorReset();
    break;
 }  

}

void loop()
{
  checkCarTimer();
}


For helpers not being members of Your club, links to hardware would be appreciated.

Where is the link to the motors? Schematics is highly wanted...

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