I2C Protocol, which stands for Inter-Integrated Circuit [3], is an excellent way for beginners to make useful connections between their motor drivers and Arduino microcontroller. The protocol relies on a common ground and two pins: signal clock (SCL) and signal data (SDA). There are two main steps.
Figure 1: Demonstration of the I2C code uploaded to Arduino Nano via Arduino IDE.
A future step for intermediate to advanced coders includes modifying the below code. Notice that each motor driver is initialized with a different value (14-19). This can be changed in the Tic Control Center, downloaded via Pololu [6].
#include
TicI2C tic1(14);
TicI2C tic2(15);
TicI2C tic3(16);
TicI2C tic4(17);
TicI2C tic5(18);
TicI2C tic6(19);
float Maxpos = 100;
float minpos = -100;
float Maxspeed = 2000000;
float minspeed = -2000000;
int x = 0;
int flag = 0;
float y = 0;
float myspeed = 0.;
float spdnow;
void setup()
{
// Set up I2C.
Wire.begin();
delay(20);
tic1.exitSafeStart();
tic2.exitSafeStart();
tic3.exitSafeStart();
tic4.exitSafeStart();
tic5.exitSafeStart();
tic6.exitSafeStart();
pinMode(6, INPUT_PULLUP);
Serial.begin(9600);
}
void resetCommandTimeout()
{
tic1.resetCommandTimeout();
tic2.resetCommandTimeout();
tic3.resetCommandTimeout();
tic4.resetCommandTimeout();
tic5.resetCommandTimeout();
tic6.resetCommandTimeout();
}
// Delays for the specified number of milliseconds while
// resetting the Tic’s command timeout so that its movement does
// not get interrupted by errors.
void delayWhileResettingCommandTimeout(uint32_t ms)
{
uint32_t start = millis();
do
{
resetCommandTimeout();
} while ((uint32_t)(millis() – start) <= ms);
}
void loop(){
// all motors work together
// travel in one direction, half-speed, for a second
tic1.setTargetVelocity(-1000000);
tic2.setTargetVelocity(-1000000);
tic3.setTargetVelocity(-1000000);
tic4.setTargetVelocity(-1000000);
tic5.setTargetVelocity(-1000000);
tic6.setTargetVelocity(-1000000);
delayWhileResettingCommandTimeout(1000);
// stop for a second
tic1.setTargetVelocity(0);
tic2.setTargetVelocity(0);
tic3.setTargetVelocity(0);
tic4.setTargetVelocity(0);
tic5.setTargetVelocity(0);
tic6.setTargetVelocity(0);
delayWhileResettingCommandTimeout(1000);
// travel in opposite direction, half-speed, for a second
tic1.setTargetVelocity(1000000);
tic2.setTargetVelocity(1000000);
tic3.setTargetVelocity(1000000);
tic4.setTargetVelocity(1000000);
tic5.setTargetVelocity(1000000);
tic6.setTargetVelocity(1000000);
delayWhileResettingCommandTimeout(1000);
// turn off again for a second
tic1.setTargetVelocity(0);
tic2.setTargetVelocity(0);
tic3.setTargetVelocity(0);
tic4.setTargetVelocity(0);
tic5.setTargetVelocity(0);
tic6.setTargetVelocity(0);
delayWhileResettingCommandTimeout(1000);
}
[1] Campbell, Scott (2016). BASICS OF THE I2C COMMUNICATION PROTOCOL. Circuit Basics. https://www.circuitbasics.com/basics-of-the-i2c-communication-protocol/
[2] How I2C Hardware Works. I2C Bus. https://www.i2c-bus.org/i2c-primer/how-i2c-hardware-works/
[3] I2C – What’s That? I2C Bus. i2c-bus.org
[4] Master Reader/Slave Sender. Arduino. https://www.arduino.cc/en/Tutorial/LibraryExamples/MasterReader
[5] Sparkfun. I2C. Sparkfun Start Something. https://learn.sparkfun.com/tutorials/i2c/all
[6] Tic Software and Drivers for Windows. Pololu. https://www.pololu.com/docs/0J71/3.1
© 2024 Duke MEMS: Experiment Design and Research Methods
Theme by Anders Noren — Up ↑
Sites@Duke Express is powered by WordPress. Read the Sites@Duke Express policies and FAQs, or request help. |