Close

New function!!! Auto-stop now works in angles too

A project log for Smart Motor Driver for Robotics

This motor driver is able to control a motor using PID by I2C. Taking precise control of a motor have never been so easy.

Danny FRDanny FR 10/12/2018 at 19:270 Comments

In this log I want to share a great new function. Somebody suggested me that it will be cool and useful that SAMI can be controlled also by sending angles, not just distance. So this is what I have done. The auto-stop function now accepts data in angles, not just as plain distance. So for example now you can ask SAMI to move 360°  directly to do a full wheel rotation instead of sending the wheel circumference as distance to obtain the same result.

This was done as an easy upgrade, the code that does this is in the side of the library. Not in SAMI firmware. So is enough to download the new library to get the new function. You should know that SAMI receives/sends travel distance by encoder ticks and them the library converts this encoder ticks to distance using the wheel diameter as described in a previous log. 

This allow us to just make a new equation for convert this encoder ticks to angles instead of distance. And it looks like this:

Where steps are the encoder ticks, gear is the gearbox relation of the motor and angle is the desired angle to move. And then we add the new functions in the Arduino library to do conversions in angles for both directions (get and set): 

int32_t SAMI_2BRobots::getAngle(void) {
   	int32_t value = (int32_t)read32(SAMI_REG_DISTANCE_0);
     	value = (int32_t) ((double)(360*(double)value)/(3*(double)getGear()));
	return value;
}

void SAMI_2BRobots::setAngle(int32_t value) {
     value = (int32_t) (((double)(3*(double)getGear())/360)*(double)value);
    }

 As you can see is the same as done in distance conversion, just using the new equation.

Here you can see a small demo, firts its moves 360°, then -180°, after that 90° and finally -270° to return to the same start position.

The new library update is already available with the example included.

Thanks for reading :)

Discussions