Code:
#include "RP6ControlLib.h"
// Always needs to be included!
#include "RP6I2CmasterTWI.h" // I2C Master Library
/*****************************************************************************/
/*****************************************************************************/
// Include our new "RP6 Control I2C Master library":
#include "RP6Control_I2CMasterLib.h"
#define SRF_LEFT_ADR 0xE4
#define SRF_RIGHT_ADR 0xE6
#define SRF_FRONT_ADR 0xE2
#define MEASURE_US_LEFT_LOW 0
#define MEASURE_US_LEFT_HIGH 1
#define MEASURE_US_RIGHT_LOW 2
#define MEASURE_US_RIGHT_HIGH 3
#define MEASURE_US_FRONT_LOW 4
#define MEASURE_US_FRONT_HIGH 5
void second(void)
{
rotate(50, LEFT, 90, true);
}
void task_SRF(void)
{
static uint8_t measureInProgress = false;
static uint8_t channel = 0;
if(!measureInProgress)
{
if(TWI_operation == I2CTWI_NO_OPERATION)
{
if(channel == 0)
I2CTWI_transmit2Bytes(SRF_LEFT_ADR, 0, 81);
else if(channel == 1)
I2CTWI_transmit2Bytes(SRF_RIGHT_ADR, 0, 81);
else if(channel == 2)
I2CTWI_transmit2Bytes(SRF_FRONT_ADR, 0, 81);
measureInProgress = true;
setStopwatch1(0);
}
}
else if(getStopwatch1() > 70)
{
if(channel == 0)
{
I2CTWI_transmitByte(SRF_LEFT_ADR, 2);
I2CTWI_requestDataFromDevice(SRF_LEFT_ADR, MEASURE_US_LEFT_HIGH, 1);
channel = 1;
}
else if(channel == 1)
{
I2CTWI_transmitByte(SRF_RIGHT_ADR, 2);
I2CTWI_requestDataFromDevice(SRF_RIGHT_ADR, MEASURE_US_RIGHT_HIGH, 1);
channel = 2;
}
else if(channel == 2)
{
I2CTWI_transmitByte(SRF_FRONT_ADR, 2);
I2CTWI_requestDataFromDevice(SRF_FRONT_ADR, MEASURE_US_FRONT_HIGH, 1);
channel = 0;
}
measureInProgress = false;
setStopwatch1(0);
}
}
uint16_t distance_left = 0;
uint16_t distance_right = 0;
uint16_t distance_front = 0;
void I2C_requestedDataReady(uint8_t dataRequestID)
{
uint8_t messageBuf[8];
static uint8_t dist_tmp;
switch(dataRequestID)
{
case MEASURE_US_LEFT_HIGH:
I2CTWI_getReceivedData( messageBuf, 2 );
dist_tmp = messageBuf[0];
I2CTWI_transmitByte(SRF_LEFT_ADR, 3);
I2CTWI_requestDataFromDevice(SRF_LEFT_ADR, MEASURE_US_LEFT_LOW, 1);
break;
case MEASURE_US_LEFT_LOW:
I2CTWI_getReceivedData( messageBuf, 2 );
distance_left = messageBuf[0] + (dist_tmp << 8);
writeString_P("DistanceL: ");
writeInteger(distance_left, DEC);
writeString_P(" cm ");
break;
case MEASURE_US_RIGHT_HIGH:
I2CTWI_getReceivedData( messageBuf, 2 );
dist_tmp = messageBuf[0];
I2CTWI_transmitByte(SRF_RIGHT_ADR, 3);
I2CTWI_requestDataFromDevice(SRF_RIGHT_ADR, MEASURE_US_RIGHT_LOW, 1);
break;
case MEASURE_US_RIGHT_LOW:
I2CTWI_getReceivedData( messageBuf, 2 );
distance_right = messageBuf[0] + (dist_tmp << 8);
writeString_P("\t\tDistanceR: ");
writeInteger(distance_right, DEC);
writeString_P(" cm\n");
break;
case MEASURE_US_FRONT_HIGH:
I2CTWI_getReceivedData( messageBuf, 2 );
dist_tmp = messageBuf[0];
I2CTWI_transmitByte(SRF_FRONT_ADR, 3);
I2CTWI_requestDataFromDevice(SRF_FRONT_ADR, MEASURE_US_FRONT_LOW, 1);
break;
case MEASURE_US_FRONT_LOW:
I2CTWI_getReceivedData( messageBuf, 2 );
distance_front = messageBuf[0] + (dist_tmp << 8);
writeString_P("\t\tDistanceF: ");
writeInteger(distance_front, DEC);
writeString_P(" cm\n");
break;
}
}
void rampe(void)
{
if (readADC(ADC_6) > 1000)
{
showScreenLCD("Rampe","erkannt");
changeDirection(FWD);
while(distance_front < 10)
{
task_SRF();
moveAtSpeed(30,30);
if (distance_front < 10)
{
second();
}
if (distance_right < 10.5)
{
moveAtSpeed(80,20);
}
if (distance_left < 10)
moveAtSpeed(20,80);
}
return 0;
}
}
void linienverfolgung(void)
{
while(readADC(ADC_6) < 1000)
{
{
{ uint16_t adc_2 = readADC(ADC_2);
setCursorPosLCD(0, 13);
writeIntegerLengthLCD(adc_2, DEC, 3);
uint16_t adc_3 = readADC(ADC_3);
setCursorPosLCD(1, 13);
writeIntegerLengthLCD(adc_3, DEC, 3);
}
writeInteger(readADC(ADC_2), 10);
writeString_P(" - ");
writeInteger(readADC(ADC_3), 10);
writeString_P("\n\r");
writeInteger(readADC(ADC_4), 10);
writeString_P(" - ");
writeInteger(readADC(ADC_5), 10);
writeString_P("\n\r");
writeInteger(readADC(ADC_6), 10);
writeString_P(" - ");
if (readADC(ADC_4) < 400)
{
moveAtSpeed(10,80);
}
if (readADC(ADC_5) < 400)
{
moveAtSpeed(80,10);
}
if (readADC(ADC_2) < 400)
{
changeDirection(LEFT);
moveAtSpeed(40,70);
}
if (readADC(ADC_3) < 400)
{
changeDirection(RIGHT);
moveAtSpeed(70,40);
}
if (readADC(ADC_2) + readADC(ADC_3) > 1025)
{
changeDirection(FWD);
moveAtSpeed(60,60);
}
if (readADC(ADC_6) > 1000)
{
moveAtSpeed(0,0);
mSleep(1000);
rampe();
}
}
}
if(bumper_left || bumper_right)
{
clearLCD();
showScreenLCD("Gegenstand","erkannt");
move(60, BWD, DIST_MM(55), true);
rotate(50, RIGHT, 50, true);
move(60, FWD, DIST_MM(200), true);
rotate(50, LEFT, 50, true);
move(60, FWD, DIST_MM(150), true);
rotate(50, LEFT, 60, true);
}
}
/**
* Prints all Sensor Values on the Serial Interface.
*/
void printAllSensorValues(void)
{
getAllSensors();
writeString_P("\nRead Sensor Values:\n");
writeString_P("PL:");writeIntegerLength(mleft_power,DEC,3);
writeString_P(" | PR:");writeIntegerLength(mright_power,DEC,3);
writeString_P(" | VL:");writeIntegerLength(mleft_speed,DEC,3);
writeString_P(" | VR:");writeIntegerLength(mright_speed,DEC,3);
writeString_P(" | DL:");writeIntegerLength(mleft_des_speed,DEC,3);
writeString_P(" | DR:");writeIntegerLength(mright_des_speed,DEC,3);
writeChar('\n');
writeString_P("DSTL:");writeIntegerLength(mleft_dist,DEC,5);
writeString_P(" | DSTR:");writeIntegerLength(mright_dist,DEC,5);
writeChar('\n');
writeString_P("LSL:");writeIntegerLength(adcLSL,DEC,4);
writeString_P(" | LSR:");writeIntegerLength(adcLSR,DEC,4);
writeString_P(" | MCL:");writeIntegerLength(adcMotorCurrentLeft,DEC,4);
writeString_P(" | MCR:");writeIntegerLength(adcMotorCurrentRight,DEC,4);
writeString_P(" | BAT:");writeIntegerLength(adcBat,DEC,4);
writeString_P(" | AD0:");writeIntegerLength(adc0,DEC,4);
writeString_P(" | AD1:");writeIntegerLength(adc1,DEC,4);
writeChar('\n');
}
/*****************************************************************************/
// 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();
// ---------------------------------------
// ---------------------------------------
I2CTWI_initMaster(100);
I2CTWI_setRequestedDataReadyHandler(I2C_requestedDataReady);
I2CTWI_setTransmissionErrorHandler(I2C_transmissionError);
sound(180,80,25);
sound(220,80,25);
setLEDs(0b1111);
showScreenLCD("", "");
mSleep(500);
showScreenLCD("", "");
mSleep(1000);
setLEDs(0b0000);
startStopwatch1();
startStopwatch2();
linienverfolgung();
while(true)
{
task_checkINT0();
task_I2CTWI();
}
return 0;
}
Marian Otte
Lesezeichen