Code:
// Includes:
#include "RP6ControlLib.h"
#include "RP6I2CmasterTWI.h"
#include "RP6Control_I2CMasterLib.h"
/*****************************************************************************/
#define I2C_RP6_BASE_ADR 10
// I2C Requests:
/**
* The I2C_requestedDataReady Event Handler
*/
void I2C_requestedDataReady(uint8_t dataRequestID)
{
checkRP6Status(dataRequestID);
}
/*****************************************************************************/
void bumpersStateChanged(void)
{
// Bumper status changed, output current state and play sounds:
writeString_P("Bumpers changed: ");
if(bumper_right && bumper_left)
{
writeString_P("MIDDLE!");
sound(200,100,0);
}
else
{
if(bumper_left)
{
writeString_P("LEFT!");
sound(200,50,10);
sound(150,20,0);
}
else if(bumper_right)
{
writeString_P("RIGHT!");
sound(200,50,10);
sound(150,20,0);
}
else
{
writeString_P("FREE!");
}
}
writeChar('\n');
}
int z; //Globale Variable
void scan (int ds)
{
writeString_P("\nDS\n");
writeInteger(ds, DEC );
int max;
do
{
sound(100,10,20);
sound(245,10,30);
sound(215,60,10);
int16_t adc2r = readADC(ADC_2);
int16_t adc3r = readADC(ADC_3);
max=adc2r-(adc3r+5); //Wert der Umgebung wird genommen
writeString_P("\nrechts SCAN\n");
writeInteger(adc2r, DEC );
writeString_P("\nlinks SCAN\n");
writeInteger(adc3r, DEC );
moveAtSpeed(50,0); //Drehe
setLEDs(0b1111);
}
while(!(max<(ds-15))); //Bis neu gemessener Wert stark von dem Übergebenen Anfangswert abweicht
}
void Snake (int dif)
{
sound(225,40,0);
sound(215,60,0);
sound(205,80,0);
sound(215,60,0);
int16_t adc2 = readADC(ADC_2);
int16_t sSnake = adc2;
int16_t adc3 = readADC(ADC_3);
int16_t sSnake2 = adc3;
int a,b;
a=(sSnake-sSnake2); //Neuer Wert wird genommen
b=0;
if (a>dif+10)
{
writeString_P("\na ist grosser dif\n");
writeInteger(a, DEC );
setLEDs(0b1100);
moveAtSpeed(50,0); //rechts drehen
b=1;
}
if(a<dif-5)
{
writeString_P("\na ist kleiner dif\n");
writeInteger(a, DEC );
setLEDs(0b0011);
moveAtSpeed(0,50); //links drehen
b=1;
}
if (b==0)
{
writeString_P("\na ist gleich dif\n");
writeInteger(a, DEC );
setLEDs(0b0110);
moveAtSpeed(40,40); //gerade fahren Richtung Feuer
z=a;
}
if((sSnake>420||sSnake2>420))
{
writeString_P("\nFeuer erreicht Löschen\n");
writeString_P("\nFeuer erreicht links\n");
writeInteger(sSnake, DEC );
writeString_P("\nFeuer erreicht rechts\n");
writeInteger(sSnake2, DEC );
moveAtSpeed(0,0);
DDRC |= IO_PC7; //PC7 als Ausgang Lüfter 5-Volt
PORTC |= IO_PC7; // Lüfter an
mSleep(500); // 0,5s
PORTC &= ~IO_PC7; // Lüfter aus
move(60,BWD, DIST_MM(200), BLOCKING);
rotate(40,RIGHT,65,BLOCKING);
mSleep(4000);
changeDirection(FWD);
adc2 = readADC(ADC_2);
sSnake = adc2;
adc3 = readADC(ADC_3);
sSnake2 = adc3;
a=(sSnake-sSnake2);
z=a;
scan(z);
}
}
// I2C Error handler
/**
* This function gets called automatically if there was an I2C Error like
* the slave sent a "not acknowledge" (NACK, error codes e.g. 0x20 or 0x30).
*/
void I2C_transmissionError(uint8_t errorState)
{
writeString_P("\nI2C ERROR - TWI STATE: 0x");
writeInteger(errorState, HEX);
writeChar('\n');
}
/*****************************************************************************/
// Main function - The program starts here:
int main(void)
{
initRP6Control();
initLCD();
writeString_P("\n\nRP6 CONTROL M32 I2C Master Example Program!\n");
writeString_P("\nMoving...\n");
DDRA &= ~ADC3;
DDRA &= ~ADC2;
// ---------------------------------------
WDT_setRequestHandler(watchDogRequest);
BUMPERS_setStateChangedHandler(bumpersStateChanged);
// ---------------------------------------
// Init TWI Interface:
I2CTWI_initMaster(100);
I2CTWI_setRequestedDataReadyHandler(I2C_requestedDataReady);
I2CTWI_setTransmissionErrorHandler(I2C_transmissionError);
sound(180,80,25);
sound(220,80,25);
setLEDs(0b1001);
// ---------------------------------------
I2CTWI_transmit3Bytes(I2C_RP6_BASE_ADR, 0, CMD_SET_ACS_POWER, ACS_PWR_MED);
I2CTWI_transmit3Bytes(I2C_RP6_BASE_ADR, 0, CMD_SET_WDT, true);
I2CTWI_transmit3Bytes(I2C_RP6_BASE_ADR, 0, CMD_SET_WDT_RQ, true);
int16_t adc2 = readADC(ADC_2);
int16_t sSnake = adc2;
writeString_P("\nadc2 MAIN\n");
writeInteger(sSnake, DEC );
int16_t adc3 = readADC(ADC_3);
int16_t sSnake2 = adc3;
writeString_P("\nadc3 MAIN\n");
writeInteger(sSnake2, DEC );
int dd;
dd=(sSnake-sSnake2); //Startwert
scan(dd);
sound(245,10,0);//Abeichung zum Startwert erkannt
sound(235,20,0);
sound(225,40,0);
sound(215,60,0);
sound(205,80,0);
sound(215,60,0);
sound(225,40,0);
sound(235,20,0);
sound(245,10,0);
setLEDs(0b0001);
sSnake=readADC(ADC_2);
writeString_P("\nneu rechts ADC2\n");
writeInteger(sSnake, DEC );
sSnake2=readADC(ADC_3);
writeString_P("\nneu links ADC3\n");
writeInteger(sSnake2, DEC );
dd=(sSnake-sSnake2); //Feuer gefunden Neuer Wert
writeString_P("\ndd zu dif\n");
writeInteger(dd, DEC );
z=dd;
while (true)
{
Snake(dd);
task_checkINT0();
task_I2CTWI();
dd=z;
}
return 0;
}
Danke für eure Hilfe
Lesezeichen