This is the code that I'm using to send data to the Arduino from Matlab.
MATLAB
freeduinoStringArray = ['xx'; 'FW'; 'RV'; 'TL'; 'TR';'BR'];
freeduinoSerial = serial('COM18');
set(freeduinoSerial, 'BaudRate', 9600, 'Terminator', 'CR/LF', 'TimeOut', 100);
fopen(freeduinoSerial);
fprintf(freeduinoSerial, '%s', freeduinoString); //freeduinoSerial contains one of the values in freeduinoStringArray
This code runs inside the main function and is called over and over again as new input is obtained by MATLAB from an image capture.
The following is the code which is used to control the motors with Arduino
/*
HERCULES Motor Controller Code
PWM control
for reverse the direction pin should be changed in the motor driver of hercules
BRAKE - BR
FORWARD - FW
REVERSE - RV
TURN LEFT - TL
TURN RIGHT - TR
IGNORE - xx
TEST - TT
/*
// Set pin 9's PWM frequency to 3906 Hz (31250/8 = 3906)
// Note that the base frequency for pins 3, 9, 10, and 11 is 31250 Hz
setPwmFrequency(9, 8 );
// Set pin 6's PWM frequency to 62500 Hz (62500/1 = 62500)
// Note that the base frequency for pins 5 and 6 is 62500 Hz
setPwmFrequency(6, 1 );
// Set pin 10's PWM frequency to 31 Hz (31250/1024 = 31)
setPwmFrequency(10, 1024) ;
Please keep in mind that changing the PWM frequency changes the Atmega's timers and disrupts the normal operation of many functions that rely on time (delay(), millis(), Servo library).
/**
* Divides a given PWM pin frequency by a divisor.
*
* The resulting frequency is equal to the base frequency divided by
* the given divisor:
* - Base frequencies:
* o The base frequency for pins 3, 9, 10, and 11 is 31250 Hz.
* o The base frequency for pins 5 and 6 is 62500 Hz.
* - Divisors:
* o The divisors available on pins 5, 6, 9 and 10 are: 1, 8, 64,
* 256, and 1024.
* o The divisors available on pins 3 and 11 are: 1, 8, 32, 64,
* 128, 256, and 1024.
*
* PWM frequencies are tied together in pairs of pins. If one in a
* pair is changed, the other is also changed to match:
* - Pins 5 and 6 are paired on timer0
* - Pins 9 and 10 are paired on timer1
* - Pins 3 and 11 are paired on timer2
*
* Note that this function will have side effects on anything else
* that uses timers:
* - Changes on pins 3, 5, 6, or 11 may cause the delay() and
* millis() functions to stop working. Other timing-related
* functions may also be affected.
* - Changes on pins 9 or 10 will cause the Servo library to function
* incorrectly.
*
*/
void setPwmFrequency(int pin, int divisor) {
byte mode;
if(pin == 5 || pin == 6 || pin == 9 || pin == 10) {
switch(divisor) {
case 1: mode = 0x01; break;
case 8: mode = 0x02; break;
case 64: mode = 0x03; break;
case 256: mode = 0x04; break;
case 1024: mode = 0x05; break;
default: return;
}
if(pin == 5 || pin == 6) {
TCCR0B = TCCR0B & 0b11111000 | mode;
} else {
TCCR1B = TCCR1B & 0b11111000 | mode;
}
} else if(pin == 3 || pin == 11) {
switch(divisor) {
case 1: mode = 0x01; break;
case 8: mode = 0x02; break;
case 32: mode = 0x03; break;
case 64: mode = 0x04; break;
case 128: mode = 0x05; break;
case 256: mode = 0x06; break;
case 1024: mode = 0x07; break;
default: return;
}
TCCR2B = TCCR2B & 0b11111000 | mode;
}
}
float fadeValue1=0;
float fadeValue2=0;
int x=0;
int y=0;
int z=0;
const int commandLength = 2;
int stepCount = 0;
int readIndex = 0;
char receivedCommand[] = {'x' , 'x'};
char prevreceivedCommand[] = {'x' , 'x'};
void setup()
{
setPwmFrequency(5, 8 );
setPwmFrequency(6, 8 );
// TCCR2B=((TCCR2B & 0xF8) | 2);
pinMode(8, OUTPUT);
pinMode(13, OUTPUT);
pinMode(6, OUTPUT);
pinMode(5, OUTPUT);
pinMode(12, OUTPUT);
digitalWrite(8, HIGH);
Serial.begin(9600);
Serial.println("READY");
}
void loop()
{
if(Serial.available())
{
prevreceivedCommand[0,1]=receivedCommand[0,1];
for( ; readIndex < commandLength; readIndex++)
{
receivedCommand[readIndex] = Serial.read();
Serial.println(receivedCommand[readIndex]);
}
Serial.println("Command received.");
switch (receivedCommand[0])
{
case 'B' :
if(receivedCommand[1] == 'R')
{
Serial.println("Received BRAKE.");
while(Serial.available() == 0)
{
relayBrake();
}
}
break;
case 'F' :
if(receivedCommand[1] == 'W')
{
Serial.println("Received FORWARD.");
{
if (prevreceivedCommand[0,1] != receivedCommand[0,1])
{relayBrake();
}
while(Serial.available() == 0)
{
digitalWrite(12, HIGH);
digitalWrite(13, HIGH);
relayForward();
}
}
}
break;
case 'R' :
if(receivedCommand[1] == 'V')
{
Serial.println("Received REVERSE.");
{
if (prevreceivedCommand[0,1] != receivedCommand[0,1])
{relayBrake();
;}
while(Serial.available() == 0)
{
digitalWrite(12, LOW);
digitalWrite(13, LOW);
relayReverse();
}
}
}
break;
case 'T' :
if(receivedCommand[1] == 'L')
{
Serial.println("Received TURN LEFT.");
{
if (prevreceivedCommand[0,1] != receivedCommand[0,1])
{relayBrake();
;}
while(Serial.available() == 0)
{
digitalWrite(12, LOW);
digitalWrite(13, HIGH);
relayTurnLeft();
}
}
}
if(receivedCommand[1] == 'R')
{
if (prevreceivedCommand[0,1] != receivedCommand[0,1])
{relayBrake();
}
Serial.println("Received TURN RIGHT.");
while(Serial.available() == 0)
{
digitalWrite(12, HIGH);
digitalWrite(13, LOW);
relayTurnRight();
}
}
break;
case 'x' :
if(receivedCommand[1] == 'x')
{
Serial.println("Ignored.");
while(Serial.available() == 0)
{
relayBrake();
}
}
break;
}
}
else
{
if(Serial.available() == 0)
{
Serial.println("READY");
}
}
readIndex = 0;
}
void relayBrake()
{
for(; fadeValue1 >= 0 || fadeValue2 >= 0 ; )
{
if(fadeValue1 >= 0)
{
analogWrite(5, fadeValue1);
fadeValue1 -=0.005;
}
if(fadeValue2 >= 0)
{
analogWrite(6, fadeValue2);
fadeValue2 -=0.005;
}
}
}
void relayReverse()
{
for( ; fadeValue1 <= 60 || fadeValue2 <= 60; )
{
if (fadeValue1 <= 60)
{
analogWrite(5, fadeValue1);
fadeValue1 +=0.005;
}
if (fadeValue2 <= 60)
{
analogWrite(6, fadeValue2);
fadeValue2 +=0.005;
}
}
}
void relayForward()
{
for( ; fadeValue1 <= 60 || fadeValue2 <= 60; )
{
if (fadeValue1 <= 60)
{
fadeValue1 +=0.005;
analogWrite(5, fadeValue1);
}
if (fadeValue2 <= 60)
{
fadeValue2 +=0.005;
analogWrite(6, fadeValue2);
}
}
}
void relayTurnLeft()
{
for(; fadeValue1 <= 60 || fadeValue2 >=0; )
{
if (fadeValue1 <= 60)
{
fadeValue1 +=0.001;
analogWrite(5, fadeValue1);
}
if (fadeValue2 >=0)
{
fadeValue2 -=0.001;
analogWrite(6, fadeValue2);
}
}
}
void relayTurnRight()
{
for(; fadeValue1 >= 0 || fadeValue2 <=60; )
{
if (fadeValue1 >= 0)
{
fadeValue1 -=0.001;
analogWrite(5, fadeValue1);
}
if (fadeValue2 <=60)
{
fadeValue2 +=0.001;
analogWrite(6, fadeValue2);
}
}
}
Thanks for the help!
