Hola a todos
Estoy haciendo un robot que por ahora funciona correctamente. El problema que tengo es que necesito usar dos servomotores a la vez que uso virtual wire y controlo un driver de motroresdoble puente en h.
Para ello uso la librería ServoTimer2. Tras googlear mucho he visto que no soy capaz de que funcione, de hecho ya no solo con el codigo de mi robot si no con un codigo de ejemplo cogido de internet.
Con mi codigo da este error(la unica diferencia entre el codigo que funciona y el que no es esta linea: #include <ServoTimer2.h>):
C:\Archivos de programa\Arduino\libraries\ServoTimer2\ServoTimer2.cpp:5:26: error: WConstants.h: No such file or directory
C:\Archivos de programa\Arduino\libraries\ServoTimer2\ServoTimer2.cpp: In function 'void __vector_15()':
C:\Archivos de programa\Arduino\libraries\ServoTimer2\ServoTimer2.cpp:35: error: 'LOW' was not declared in this scope
C:\Archivos de programa\Arduino\libraries\ServoTimer2\ServoTimer2.cpp:35: error: 'digitalWrite' was not declared in this scope
C:\Archivos de programa\Arduino\libraries\ServoTimer2\ServoTimer2.cpp:42: error: 'HIGH' was not declared in this scope
C:\Archivos de programa\Arduino\libraries\ServoTimer2\ServoTimer2.cpp:42: error: 'digitalWrite' was not declared in this scope
C:\Archivos de programa\Arduino\libraries\ServoTimer2\ServoTimer2.cpp: In member function 'uint8_t ServoTimer2::attach(int)':
C:\Archivos de programa\Arduino\libraries\ServoTimer2\ServoTimer2.cpp:65: error: 'OUTPUT' was not declared in this scope
C:\Archivos de programa\Arduino\libraries\ServoTimer2\ServoTimer2.cpp:65: error: 'pinMode' was not declared in this scope
Y aqui mi codigo:
#include <VirtualWire.h>
#include <ServoTimer2.h>
// This code have extracts from differents tutorials on the web that I used long time ago, so I can remmeber the authors
#define MOTOR1_CTL1 4 // I1
#define MOTOR1_CTL2 3 // I2
#define MOTOR1_PWM 2 // EA
#define MOTOR2_CTL1 7 // I3
#define MOTOR2_CTL2 6// I4
#define MOTOR2_PWM 5 // EB
#define MOTOR_DIR_FORWARD 0
#define MOTOR_DIR_BACKWARD 1
// Sensors
int Sensor1Data;
int Sensor2Data;
int Servo1;
int Servo2;
// RF Transmission container
char Sensor1CharMsg[17];
//ServoTimer2 one;
//ServoTimer2 two;
void setup() {
Serial.begin(9600);
pinMode(MOTOR1_CTL1,OUTPUT);
pinMode(MOTOR1_CTL2,OUTPUT);
pinMode(MOTOR1_PWM,OUTPUT);
// Setup pins for motor 2
pinMode(MOTOR2_CTL1,OUTPUT);
pinMode(MOTOR2_CTL2,OUTPUT);
pinMode(MOTOR2_PWM,OUTPUT);
// sets the digital pin as output
pinMode(13, OUTPUT);
one.attach(9);
two.attach(10);
// VirtualWire
// Initialise the IO and ISR
// Required for DR3100
// Bits per sec
vw_setup(2000);
vw_set_rx_pin(11);
// Start the receiver PLL running
vw_rx_start();
} // END void setup
void loop(){
uint8_t buf[VW_MAX_MESSAGE_LEN];
uint8_t buflen = VW_MAX_MESSAGE_LEN;
//Taking the data from the control base
if (vw_get_message(buf, &buflen))
{
int i;
// Turn on a light to show received good message
digitalWrite(13, true);
// Message with a good checksum received, dump it.
for (i = 0; i < buflen; i++)
{
// Fill Sensor1CharMsg Char array with corresponding
// chars from buffer.
Sensor1CharMsg[i] = char(buf[i]);
}
sscanf(Sensor1CharMsg, "%d,%d,%d,%d",&Sensor2Data, &Sensor1Data,&Servo1,&Servo2); // Convierte un string en un array
// Turn off light to and await next message
digitalWrite(13, false);
}
// Control the motors
if (Sensor1Data > 700){
int v1 = 255;//map(Sensor1Data,700,1024,150,250);
motorStart(1, MOTOR_DIR_FORWARD);
setSpeed(1, v1);
motorStart(2, MOTOR_DIR_FORWARD);
setSpeed(2, v1);
delay(2);
}
if (Sensor1Data < 300){
int v2 = 255;//
map(Sensor1Data,0,300,250,150);
motorStart(1, MOTOR_DIR_BACKWARD);
setSpeed(1, v2);
motorStart(2, MOTOR_DIR_BACKWARD);
setSpeed(2, v2);
delay(2);
}
if(300<Sensor1Data<700 && 300 < Sensor2Data < 700){
motorStop(1);
motorStop(2);
}
if(300<Sensor1Data<700){
if (Sensor2Data > 700){
int v1 = map(Sensor1Data,700,1024,150,250);
motorStart(2, MOTOR_DIR_FORWARD);
setSpeed(1, 255);
motorStart(1, MOTOR_DIR_BACKWARD);
setSpeed(2, 255);
delay(2);
}
if (Sensor2Data < 300){
int v2 = map(Sensor1Data,0,300,250,150);
motorStart(2, MOTOR_DIR_BACKWARD);
setSpeed(1, 255);
motorStart(1, MOTOR_DIR_FORWARD);
setSpeed(2, 255);
delay(2);
}
}
//End controling the motors
//cabezamovil();
memset( Sensor1CharMsg, 0, sizeof( Sensor1CharMsg));// This line is for reset the Sensor1CharMsg
}
Son dos paginas
void setSpeed(char motor_num, char motor_speed)
{
if (motor_num == 1)
{
analogWrite(MOTOR1_PWM, motor_speed);
}
else
{
analogWrite(MOTOR2_PWM, motor_speed);
}
}
void motorStart(char motor_num, byte direction)
{
char pin_ctl1;
char pin_ctl2;
if (motor_num == 1)
{
pin_ctl1 = MOTOR1_CTL1;
pin_ctl2 = MOTOR1_CTL2;
}
else
{
pin_ctl1 = MOTOR2_CTL1;
pin_ctl2 = MOTOR2_CTL2;
}
switch (direction)
{
case MOTOR_DIR_FORWARD:
{
digitalWrite(pin_ctl1,LOW);
digitalWrite(pin_ctl2,HIGH);
}
break;
case MOTOR_DIR_BACKWARD:
{
digitalWrite(pin_ctl1,HIGH);
digitalWrite(pin_ctl2,LOW);
}
break;
}
}
void motorStop(char motor_num)
{
setSpeed(motor_num, 0);
if (motor_num == 1)
{
digitalWrite(MOTOR1_CTL1,HIGH);
digitalWrite(MOTOR1_CTL2,HIGH);
}
else
{
digitalWrite(MOTOR2_CTL1,HIGH);
digitalWrite(MOTOR2_CTL2,HIGH);
}
} ]
Espero que me podais ayudar, creo que he incluido toda la información posible si necesitáis mas pedirla