diff --git a/Code/miniRobot/miniRobot.ino b/Code/miniRobot/miniRobot.ino new file mode 100644 index 0000000..50c4323 --- /dev/null +++ b/Code/miniRobot/miniRobot.ino @@ -0,0 +1,155 @@ +/* + * modularer Mini Roboter mit diversen Sensoren + * + * + */ +//Funk +#include +#include +#include +#define CE A0 +#define CSN 3 +RF24 radio(A0, 3); // CE, CSN + +byte commands[32]; //byte 0 = command + +void inline clearCommands() { + for(uint8_t i=0; i<32; i++) { + commands[i] = 0xFF; + } +} + +const byte address[6] = "00001"; +//Kommandos +#define nothing 9 //reset/nichts tun +#define speedA 1 // set speed A + speed +#define dirA 2 // set direction A + dir +#define speedB 3 // set speed B + speed +#define dirB 4 // set direction B + dir +#define goDrive 5 //go + time to go +#define stopDrive 6 //stop +#define getTemp 7 //get temperature +#define timeToDrive 8 //Zeitdauer des fahrens +//Motortreiber +//#include +#include + +#define BEEP 14 + +#define PWM_PINA 10 +#define PINA 8 +#define PWM_PINB 9 +#define PINB 7 +#define NUMPWM 1 + +#define RESOLUTION 255 + +//MX1508 motorA(PWM_PINA,PINA, FAST_DECAY, NUMPWM); +//MX1508 motorB(PWM_PINB,PINB, FAST_DECAY, NUMPWM); +L298N drive; + + +volatile int pwmA = 0; +volatile int pwmB = 0; +bool forwardA = true; +bool forwardB = true; +volatile bool driveOn = false; + +int temperatur = 0; + + +volatile long driveTimeout = 0; +volatile long driveTimeDiff = 0; + +void setup() { + Serial.begin(115200); +// motorA.setPWM16(2,RESOLUTION); +// motorB.setPWM16(2,RESOLUTION); + radio.begin(); + radio.openReadingPipe(0, address); + radio.setPALevel(RF24_PA_MAX); + radio.startListening(); + clearCommands(); +} + +void loop() { + if (radio.available()) { + radio.read(&commands, sizeof(commands)); + commandInterpretation(); + } + //Serial.println(driveOn); + if(((millis() - driveTimeDiff) > driveTimeout)) { + pwmA = 0; + pwmB = 0; + } + drive.setPWM_A(pwmA); + drive.setPWM_B(pwmB); +} + +void commandInterpretation() { + for(uint8_t i = 0; i < 28; i += 3) { + switch(commands[i]) { + case nothing : { + pwmA = 0; + pwmB = 0; + forwardA = true; + forwardB = true; + driveOn = false; + break; + } + case speedA : { + int temp1; + temp1 = (0xFF00 & (commands[i+1] << 8)); + temp1 |= (0x00FF & commands[i+2]); + pwmA = temp1; + break; + } + case dirA : { + bool temp2 = commands[i+2]; + break; + } + case speedB : { + int temp3; + temp3 = (0xFF00 & (commands[i+1] << 8)); + temp3 |= (0x00FF & commands[i+2]); + pwmB = temp3; + break; + } + case dirB : { + bool temp4; + temp4 = commands[i+2]; + break; + } + case goDrive : { + driveOn = true; + break; + } + case stopDrive : { + driveOn = false; + break; + } + case getTemp : { + temperatur = (0xFF00 & (commands[i+1] << 8)); + temperatur |= (0x00FF & commands[i+2]); + break; + } + case timeToDrive : { + uint16_t driveTime = 0; + driveTime = (0xFF00 & (commands[i+1] << 8)); + driveTime |= (0x00FF & commands[i+2]); + driveTimeout = (long)driveTime; + driveTimeDiff = millis(); + Serial.println(driveTimeout); + break; + } + default : { /* pwmA = 0; + pwmB = 0; + forwardA = true; + forwardB = true; + driveOn = false; */ + break; + } + } + } + clearCommands(); +} diff --git a/Code/miniRobotRC/_main.ino b/Code/miniRobotRC/_main.ino new file mode 100644 index 0000000..5ae806e --- /dev/null +++ b/Code/miniRobotRC/_main.ino @@ -0,0 +1,115 @@ +#include +#include +#include + +void inline clearCommands() { + for(uint8_t i=0; i<32; i++) { + commands[i] = 0xFF; + } +} + +void setup() { + remoteControlInit(); + funkInit(); + Serial.begin(115200); + driveTimeout = 50; +/* + commands[0] = speedA; + commands[1] = 0xFF & (pwmA >> 8); + commands[2] = 0xFF & pwmA; + + commands[3] = speedB; + commands[4] = 0xFF & (pwmB >> 8); + commands[5] = 0xFF & pwmB; + + commands[6] = goDrive; + + commands[9] = timeToDrive; + commands[10] = 0xFF & (driveTimeout >> 8); + commands[11] = 0xFF & driveTimeout; */ + clearCommands(); +} + +void loop() { +//char text[32] = "Hello"; + // radio.write(&commands, sizeof(commands)); + //lcdMenu(); + //delay(10000); + manualDrive(); +} + +void lcdMenu() { + lcd.println("Platzhalter"); + + if(tasten.getButtonCycle(buttonUp)) { + + tasten.clearAllButtons(); + } + if(tasten.getButtonCycle(buttonDown)) { + + tasten.clearAllButtons(); + } + +} + +void manualDrive() { + bool goOn = false; + while(!tasten.getButtonCycle(buttonStart)) { + clearCommands(); + if(!tasten.getAnyPressed()) { + lcd.clear(); + lcd.println("Warte..."); + } + if(tasten.checkButton(buttonB) || tasten.checkButton(buttonUp)) { + pwmA = -215; + pwmB = -255; + // tasten.clearButton(buttonB); + lcd.clear(); + lcd.println("geradeaus fahren"); + goOn =true; + } + if(tasten.checkButton(buttonC) || tasten.checkButton(buttonDown)) { + pwmA = 100; + pwmB = 255; + // tasten.clearButton(buttonC); + lcd.clear(); + lcd.println("rueckwaerts fahren"); + goOn =true; + } + if(tasten.checkButton(buttonRight)) { + pwmA = -100; //rechter Motor + pwmB = 100; + //tasten.clearButton(buttonRight); + lcd.clear(); + lcd.println("rechts lenken"); + goOn =true; + } + if(tasten.checkButton(buttonLeft)) { + pwmB = -100; + pwmA = 100; + //tasten.clearButton(buttonLeft); + lcd.clear(); + lcd.println("links lenken"); + goOn =true; + } + if(goOn) { + commands[0] = speedA; + commands[1] = highByte(pwmA); + commands[2] = lowByte(pwmA); + commands[3] = speedB; + commands[4] = highByte(pwmB); + commands[5] = lowByte(pwmB); + commands[6] = timeToDrive; + commands[7] = highByte(driveTimeout); + commands[8] = lowByte(driveTimeout); + commands[9] = goDrive; + radio.write(&commands, sizeof(commands)); + goOn = false; + } + } + tasten.clearAllButtons(); +} + +ISR(TIMER2_COMPA_vect) { // called by timer2 + tasten.checkButtons(); //Aufruf 1-mal pro Millisekunde +} diff --git a/Code/miniRobotRC/funkInit.ino b/Code/miniRobotRC/funkInit.ino new file mode 100644 index 0000000..308995f --- /dev/null +++ b/Code/miniRobotRC/funkInit.ino @@ -0,0 +1,6 @@ +void funkInit() { + radio.begin(); + radio.openWritingPipe(address); + radio.setPALevel(RF24_PA_MAX); + radio.stopListening(); +} diff --git a/Code/miniRobotRC/miniRobotRC.ino b/Code/miniRobotRC/miniRobotRC.ino new file mode 100644 index 0000000..a73b7bc --- /dev/null +++ b/Code/miniRobotRC/miniRobotRC.ino @@ -0,0 +1,68 @@ +//template +#include +#include +#include + +#define CLR_BIT(p,n) ((p) &= ~((1) << (n))) +#define SET_BIT(p,n) ((p) |= (1 << (n))) + +#define BL 10 //backlight, Hintergrundbeleuchtung LCD +#define SD_CARD_CS 2 //D2 ist Chip Enable +#define BAT_VOLTAGE A7 //Akkuspannung an A7 +#define _OE_LEVEL 7 //D7 fuer Output Enable Pegelwandler +#define BRIGHTNESS OCR1B +//globale Objekte anlegen +volatile PCD8544_SPI lcd; +volatile myInterrupts Timer2; +volatile myInterrupts pwmBL; +volatile shiftRegButton tasten; + +//Kommandos +#define nothing 9 //reset/nichts tun +#define speedA 1 // set speed A + speed +#define dirA 2 // set direction A + dir +#define speedB 3 // set speed B + speed +#define dirB 4 // set direction B + dir +#define goDrive 5 //go + time to go +#define stopDrive 6 //stop +#define getTemp 7 //get temperature +#define timeToDrive 8 //Zeitdauer des fahrens + +int pwmA = 0; +int pwmB = 0; +bool forwardA = true; +bool forwardB = true; +bool driveOn = false; +uint16_t driveTimeout = 0; +//#define highByte(x) ( (x) >> (8) ) // keep upper 8 bits +//#define lowByte(x) ( (x) & (0xff) ) // keep lower 8 bits + +#include +#include +#include + +RF24 radio(A2, A3); // CE, CSN + +const byte address[6] = "00001"; + +uint8_t commands[32]; + +const String mainMenu[] = { + "fahren", //0 + "stoppen", //1 + "manuell fahren", //2 + "", //3 + "", //4 + "", //5 + "", //6 + "", //7 + "", //8 + "" //9 +}; + +const String subMenuEntry0[] = { + "Zeitdauer", + "Speed A", + "Speed B", + "Starten" +}; diff --git a/Code/miniRobotRC/remoteControlInit.ino b/Code/miniRobotRC/remoteControlInit.ino new file mode 100644 index 0000000..0eef2eb --- /dev/null +++ b/Code/miniRobotRC/remoteControlInit.ino @@ -0,0 +1,18 @@ +void remoteControlInit() { +//Timer2 fuer 1ms initialisieren + Timer2.initTimer2(); + Timer2.enableTimer2Interrupt(); +//Hintergrundbeleuchtung pwm + pwmBL.initOCR1B(); +//Pegelwandler ein + pinMode(_OE_LEVEL, OUTPUT); + digitalWrite(_OE_LEVEL, HIGH); +//Hintergrundbeleuchtung an, halbe Helligkeit + pinMode(BL, OUTPUT); + BRIGHTNESS = 30; +//LCD init, invert, vop, tempcoef, bias + lcd.begin(false, 0xB1, 0x04, 0x12); + lcd.clear(); +//erster Taster check + tasten.checkButtons(); +}