├── .gitattributes ├── Chapter02 ├── Blink.cpp ├── SmartLight.cpp └── Soft_PWM_Blink.cpp ├── Chapter03 ├── .gitattributes ├── ConstructionOfRobotVideo.mp4 ├── Forward.cpp └── RobotMovement.cpp ├── Chapter04 ├── DistanceMeasurement.cpp ├── I2CLCD.cpp ├── I2CLCDdm.cpp ├── LCDdisplay.cpp ├── LCDdm.cpp └── ObstacleAvoiderRobot.cpp ├── Chapter05 ├── Hello_World.cpp ├── LEDBuzzer.cpp ├── LEDOnOff │ ├── LEDOnOff.pro │ ├── LEDOnOff.pro.user │ ├── main.cpp │ ├── mainwindow.cpp │ ├── mainwindow.h │ └── mainwindow.ui ├── Laptop_Controlled_Rover.cpp └── QTRover │ ├── QTRover.pro │ ├── QTRover.pro.user │ ├── main.cpp │ ├── mainwindow.cpp │ ├── mainwindow.h │ └── mainwindow.ui ├── Chapter06 └── DisplayImage.cpp ├── Chapter07 ├── BallTracing.cpp ├── Camerafeed.cpp ├── ObjectDetection.cpp ├── ObjectFollowingRobot.cpp └── ObjectFollowingRobotwithoutUltrasonic.cpp ├── Chapter08 ├── FaceDetection.cpp ├── Facetrackingrobot.cpp └── SmilingFace.cpp ├── Chapter09 ├── Mic.png ├── TalkingPi.aia ├── VoiceBot.c └── VoiceControlBot.aia ├── LICENSE └── README.md /.gitattributes: -------------------------------------------------------------------------------- 1 | *.mp4 filter=lfs diff=lfs merge=lfs -text 2 | .*mp4 filter=lfs diff=lfs merge=lfs -text 3 | -------------------------------------------------------------------------------- /Chapter02/Blink.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(void) 5 | { 6 | wiringPiSetup(); 7 | pinMode(15,OUTPUT); 8 | 9 | for(;;) 10 | { 11 | digitalWrite(15,HIGH); 12 | delay(1000); 13 | digitalWrite(15,LOW); 14 | delay(1000); 15 | } 16 | return 0; 17 | } -------------------------------------------------------------------------------- /Chapter02/SmartLight.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(void) 5 | { 6 | 7 | wiringPiSetup(); 8 | 9 | pinMode(0,OUTPUT); 10 | pinMode(8,INPUT); 11 | 12 | for(;;) 13 | { 14 | int ldrstate = digitalRead(8); 15 | if(ldrstate == HIGH) 16 | { 17 | digitalWrite(0,HIGH); 18 | } 19 | else 20 | { 21 | digitalWrite(0,LOW); 22 | } 23 | } 24 | return 0; 25 | } -------------------------------------------------------------------------------- /Chapter02/Soft_PWM_Blink.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | int main(void) 5 | { 6 | wiringPiSetup(); 7 | softPwmCreate (15, 0, 100) ; 8 | for(;;) 9 | { 10 | softPwmWrite (15, 25); 11 | delay(1000); 12 | softPwmWrite (15, 0); 13 | delay(1000); 14 | softPwmWrite (15, 50); 15 | delay(1000); 16 | softPwmWrite (15, 0); 17 | delay(1000); 18 | softPwmWrite (15, 100); 19 | delay(1000); 20 | softPwmWrite (15, 0); 21 | delay(1000); 22 | } 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /Chapter03/.gitattributes: -------------------------------------------------------------------------------- 1 | .*mp4 filter=lfs diff=lfs merge=lfs -text 2 | -------------------------------------------------------------------------------- /Chapter03/ConstructionOfRobotVideo.mp4: -------------------------------------------------------------------------------- 1 | version https://git-lfs.github.com/spec/v1 2 | oid sha256:21dd56529413600413a5358808db2242f01c735ecbc77b74c1446f4770d4808e 3 | size 155886600 4 | -------------------------------------------------------------------------------- /Chapter03/Forward.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(void) 5 | { 6 | wiringPiSetup(); 7 | pinMode(0,OUTPUT); 8 | pinMode(2,OUTPUT); 9 | pinMode(3,OUTPUT); 10 | pinMode(4,OUTPUT); 11 | 12 | 13 | for(int i=0; i<1;i++) 14 | { 15 | digitalWrite(0,HIGH); //PIN O & 2 will move the Left Motor 16 | digitalWrite(2,LOW); 17 | digitalWrite(3,HIGH); //PIN 3 & 4 will move the Right Motor 18 | digitalWrite(4,LOW); 19 | delay(3000); 20 | } 21 | return 0; 22 | } -------------------------------------------------------------------------------- /Chapter03/RobotMovement.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(void) 5 | { 6 | wiringPiSetup(); 7 | pinMode(0,OUTPUT); 8 | pinMode(2,OUTPUT); 9 | pinMode(3,OUTPUT); 10 | pinMode(4,OUTPUT); 11 | 12 | 13 | for(int i=0; i<1;i++) 14 | { 15 | 16 | /* FORWARD */ 17 | digitalWrite(0,HIGH); //PIN O & 2 will move the Left Motor Forward 18 | digitalWrite(2,LOW); 19 | digitalWrite(3,HIGH); //PIN 3 & 4 will move the Right Motor Forward 20 | digitalWrite(4,LOW); 21 | delay(3000); 22 | 23 | /* BACKWARD */ 24 | digitalWrite(0,LOW); 25 | digitalWrite(2,HIGH); 26 | digitalWrite(3,LOW); 27 | digitalWrite(4,HIGH); 28 | delay(3000); 29 | 30 | /* STOP */ 31 | digitalWrite(0,HIGH); 32 | digitalWrite(2,HIGH); 33 | digitalWrite(3,HIGH); 34 | digitalWrite(4,HIGH); 35 | delay(3000); 36 | 37 | /* AXIAL LEFT TURN */ 38 | digitalWrite(0,LOW); 39 | digitalWrite(2,HIGH); 40 | digitalWrite(3,HIGH); 41 | digitalWrite(4,LOW); 42 | delay(500); 43 | 44 | /* AXIAL RIGHT TURN */ 45 | digitalWrite(0,HIGH); 46 | digitalWrite(2,LOW); 47 | digitalWrite(3,LOW); 48 | digitalWrite(4,HIGH); 49 | delay(500); 50 | 51 | 52 | /* RADIAL LEFT TURN */ 53 | digitalWrite(0,HIGH); 54 | digitalWrite(2,HIGH); 55 | digitalWrite(3,HIGH); 56 | digitalWrite(4,LOW); 57 | delay(1000); 58 | 59 | 60 | /* RADIAL RIGHT TURN */ 61 | digitalWrite(0,HIGH); 62 | digitalWrite(2,LOW); 63 | digitalWrite(3,HIGH); 64 | digitalWrite(4,HIGH); 65 | delay(1000); 66 | 67 | /* STOP */ 68 | digitalWrite(0,HIGH); 69 | digitalWrite(2,HIGH); 70 | digitalWrite(3,HIGH); 71 | digitalWrite(4,HIGH); 72 | delay(1000); 73 | } 74 | return 0; 75 | } 76 | -------------------------------------------------------------------------------- /Chapter04/DistanceMeasurement.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | #define trigger 12 8 | #define echo 13 9 | 10 | long startTime; 11 | long stopTime; 12 | 13 | int main() 14 | { 15 | 16 | wiringPiSetup(); 17 | 18 | pinMode(trigger,OUTPUT); 19 | pinMode(echo, INPUT); 20 | 21 | for(;;){ 22 | digitalWrite(trigger,LOW); 23 | delay(500); 24 | 25 | digitalWrite(trigger,HIGH); 26 | delayMicroseconds(10); 27 | 28 | digitalWrite(trigger,LOW); 29 | 30 | while(digitalRead(echo) == LOW); 31 | startTime = micros(); 32 | 33 | while(digitalRead(echo) == HIGH); 34 | stopTime = micros(); 35 | 36 | long totalTime= stopTime - startTime; 37 | float distance = (totalTime * 0.034)/2; 38 | 39 | cout << "Distance is: " << distance << " cm"< 2 | #include 3 | #include 4 | #include 5 | 6 | // Definining device parameters 7 | #define I2C_DEVICE_ADDRESS 0x27 // I2C device address 8 | #define firstrow 0x80 // 1st line 9 | #define secondrow 0xC0 // 2nd line 10 | 11 | // Define some device constants 12 | #define LCD_CHR 1 // Mode - Sending data 13 | #define LCD_CMD 0 // Mode - Sending command 14 | 15 | #define LCD_BACKLIGHT 0x08 16 | 17 | #define ENABLE 0b00000100 // Enable bit 18 | 19 | void init_lcd(void); 20 | void lcd_byte(int bits, int mode); 21 | void lcd_toggle_enable(int bits); 22 | void printmessage(const char *s); 23 | void moveCursor(int line); 24 | void clear(void); 25 | void typeChar(char val); 26 | void printInt(int i); 27 | void printFloat(float myFloat); 28 | int lcdaddr; 29 | 30 | int main() { 31 | wiringPiSetup(); 32 | lcdaddr = wiringPiI2CSetup(I2C_DEVICE_ADDRESS); 33 | init_lcd(); // initializing or setting up the LCD 34 | for(;;) { 35 | moveCursor(firstrow); 36 | printmessage("LCD OUTPUT"); 37 | moveCursor(secondrow); 38 | printmessage("USING I2C"); 39 | delay(2000); 40 | clear(); 41 | 42 | moveCursor(firstrow); 43 | printmessage("Integer: "); 44 | int iNumber = 314; 45 | printInt(iNumber); 46 | delay(2000); 47 | 48 | moveCursor(secondrow); 49 | printmessage("Float "); 50 | float fNumber = 3.14; 51 | printFloat(fNumber); 52 | delay(2000); 53 | clear(); 54 | } 55 | return 0; 56 | } 57 | 58 | 59 | void printFloat(float myFloat) { 60 | char buffer[20]; 61 | sprintf(buffer, "%4.2f", myFloat); 62 | printmessage(buffer); 63 | } 64 | 65 | 66 | void printInt(int i) { 67 | char array1[20]; 68 | sprintf(array1, "%d", i); 69 | printmessage(array1); 70 | } 71 | 72 | // clr lcd go home loc 0x80 73 | void clear(void) { 74 | lcd_byte(0x01, LCD_CMD); 75 | lcd_byte(0x02, LCD_CMD); 76 | } 77 | 78 | 79 | void moveCursor(int line) { 80 | lcd_byte(line, LCD_CMD); 81 | } 82 | 83 | 84 | void typeChar(char val) { 85 | 86 | lcd_byte(val, LCD_CHR); 87 | } 88 | 89 | 90 | 91 | void printmessage(const char *s) { 92 | 93 | while ( *s ) lcd_byte(*(s++), LCD_CHR); 94 | 95 | } 96 | 97 | void lcd_byte(int bits, int mode) { 98 | 99 | 100 | int bits_high; 101 | int bits_low; 102 | 103 | bits_high = mode | (bits & 0xF0) | LCD_BACKLIGHT; 104 | bits_low = mode | ((bits << 4) & 0xF0) | LCD_BACKLIGHT; 105 | 106 | 107 | wiringPiI2CReadReg8(lcdaddr, bits_high); 108 | lcd_toggle_enable(bits_high); 109 | 110 | 111 | wiringPiI2CReadReg8(lcdaddr, bits_low); 112 | lcd_toggle_enable(bits_low); 113 | } 114 | 115 | void lcd_toggle_enable(int bits) { 116 | 117 | delayMicroseconds(500); 118 | wiringPiI2CReadReg8(lcdaddr, (bits | ENABLE)); 119 | delayMicroseconds(500); 120 | wiringPiI2CReadReg8(lcdaddr, (bits & ~ENABLE)); 121 | delayMicroseconds(500); 122 | } 123 | 124 | 125 | void init_lcd() { 126 | 127 | lcd_byte(0x33, LCD_CMD); 128 | lcd_byte(0x32, LCD_CMD); 129 | lcd_byte(0x06, LCD_CMD); 130 | lcd_byte(0x0C, LCD_CMD); 131 | lcd_byte(0x28, LCD_CMD); 132 | lcd_byte(0x01, LCD_CMD); 133 | delayMicroseconds(500); 134 | } 135 | 136 | -------------------------------------------------------------------------------- /Chapter04/I2CLCDdm.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | // Definining device parameters 9 | #define I2C_DEVICE_ADDRESS 0x27 // I2C device address 10 | #define firstrow 0x80 // 1st line 11 | #define secondrow 0xC0 // 2nd line 12 | 13 | // Define some device constants 14 | #define LCD_CHR 1 // Mode - Sending data 15 | #define LCD_CMD 0 // Mode - Sending command 16 | 17 | #define LCD_BACKLIGHT 0x08 18 | 19 | #define ENABLE 0b00000100 // Enable bit 20 | 21 | #define trigger 12 22 | #define echo 13 23 | 24 | long startTime; 25 | long stopTime; 26 | 27 | 28 | void init_lcd(void); 29 | void lcd_byte(int bits, int mode); 30 | void lcd_toggle_enable(int bits); 31 | void printmessage(const char *s); 32 | void moveCursor(int line); 33 | void clear(void); 34 | void typeChar(char val); 35 | void printInt(int i); 36 | void printFloat(float myFloat); 37 | int lcdaddr; 38 | 39 | int main() { 40 | wiringPiSetup(); 41 | lcdaddr = wiringPiI2CSetup(I2C_DEVICE_ADDRESS); 42 | init_lcd(); //initializing or setting up the LCD 43 | 44 | pinMode(trigger,OUTPUT); 45 | pinMode(echo, INPUT); 46 | 47 | 48 | for(;;) { 49 | 50 | digitalWrite(trigger,LOW); 51 | delay(500); 52 | 53 | digitalWrite(trigger,HIGH); 54 | delayMicroseconds(10); 55 | 56 | digitalWrite(trigger,LOW); 57 | 58 | while(digitalRead(echo) == LOW); 59 | startTime = micros(); 60 | 61 | while(digitalRead(echo) == HIGH); 62 | stopTime = micros(); 63 | 64 | long totalTime= stopTime - startTime; 65 | float distance = (totalTime * 0.034)/2; 66 | 67 | 68 | moveCursor(firstrow); 69 | printmessage("Distance: "); 70 | 71 | moveCursor(secondrow); 72 | printFloat(distance); 73 | printmessage(" cm"); 74 | delay(2000); 75 | clear(); 76 | 77 | } 78 | return 0; 79 | } 80 | 81 | 82 | void printFloat(float myFloat) { 83 | char buffer[20]; 84 | sprintf(buffer, "%4.2f", myFloat); 85 | printmessage(buffer); 86 | } 87 | 88 | 89 | void printInt(int i) { 90 | char array1[20]; 91 | sprintf(array1, "%d", i); 92 | printmessage(array1); 93 | } 94 | 95 | // clr lcd go home loc 0x80 96 | void clear(void) { 97 | lcd_byte(0x01, LCD_CMD); 98 | lcd_byte(0x02, LCD_CMD); 99 | } 100 | 101 | 102 | void moveCursor(int line) { 103 | lcd_byte(line, LCD_CMD); 104 | } 105 | 106 | 107 | void typeChar(char val) { 108 | 109 | lcd_byte(val, LCD_CHR); 110 | } 111 | 112 | 113 | 114 | void printmessage(const char *s) { 115 | 116 | while ( *s ) lcd_byte(*(s++), LCD_CHR); 117 | 118 | } 119 | 120 | void lcd_byte(int bits, int mode) { 121 | 122 | 123 | int bits_high; 124 | int bits_low; 125 | 126 | bits_high = mode | (bits & 0xF0) | LCD_BACKLIGHT; 127 | bits_low = mode | ((bits << 4) & 0xF0) | LCD_BACKLIGHT; 128 | 129 | 130 | wiringPiI2CReadReg8(lcdaddr, bits_high); 131 | lcd_toggle_enable(bits_high); 132 | 133 | 134 | wiringPiI2CReadReg8(lcdaddr, bits_low); 135 | lcd_toggle_enable(bits_low); 136 | } 137 | 138 | void lcd_toggle_enable(int bits) { 139 | 140 | delayMicroseconds(500); 141 | wiringPiI2CReadReg8(lcdaddr, (bits | ENABLE)); 142 | delayMicroseconds(500); 143 | wiringPiI2CReadReg8(lcdaddr, (bits & ~ENABLE)); 144 | delayMicroseconds(500); 145 | } 146 | 147 | 148 | void init_lcd() { 149 | 150 | lcd_byte(0x33, LCD_CMD); 151 | lcd_byte(0x32, LCD_CMD); 152 | lcd_byte(0x06, LCD_CMD); 153 | lcd_byte(0x0C, LCD_CMD); 154 | lcd_byte(0x28, LCD_CMD); 155 | lcd_byte(0x01, LCD_CMD); 156 | delayMicroseconds(500); 157 | } 158 | 159 | 160 | -------------------------------------------------------------------------------- /Chapter04/LCDdisplay.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #define RS 22 //Register Select 5 | #define E 26 //Enable 6 | 7 | #define D4 24 //Data pin 4 8 | #define D5 25 //Data pin 5 9 | #define D6 27 //Data pin 6 10 | #define D7 28 //Data pin 7 11 | 12 | int main() 13 | { 14 | 15 | int fd; 16 | wiringPiSetup(); 17 | fd= lcdInit (2, 16, 4, RS, E, D4, D5, D6, D7, 0, 0, 0, 0); 18 | lcdPuts(fd, "LCD OUTPUT"); 19 | 20 | } -------------------------------------------------------------------------------- /Chapter04/LCDdm.cpp: -------------------------------------------------------------------------------- 1 | //LCD_ULTRASONIC SENSOR CODE 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | using namespace std; 9 | 10 | #define trigger 12 11 | #define echo 13 12 | #define RS 22 //Register Select 13 | #define E 26 //Enable 14 | #define D4 24 //Data pin 4 15 | #define D5 25 //Data pin 5 16 | #define D6 27 //Data pin 6 17 | #define D7 28 //Data pin 7 18 | 19 | long startTime; 20 | long stopTime; 21 | 22 | int main() 23 | { 24 | wiringPiSetup(); 25 | 26 | pinMode(trigger,OUTPUT); 27 | pinMode(echo, INPUT); 28 | 29 | int fd; 30 | wiringPiSetup(); 31 | fd= lcdInit (2, 16, 4, RS, E, D4, D5, D6, D7, 0, 0, 0, 0); 32 | lcdPuts(fd, "LCD OUTPUT"); 33 | 34 | for(;;) 35 | { 36 | digitalWrite(trigger,LOW); 37 | delay(500); 38 | 39 | digitalWrite(trigger,HIGH); 40 | delayMicroseconds(10); 41 | 42 | digitalWrite(trigger,LOW); 43 | 44 | while(digitalRead(echo) == LOW); 45 | startTime = micros(); 46 | 47 | while(digitalRead(echo) == HIGH); 48 | stopTime = micros(); 49 | 50 | long totalTime= stopTime - startTime; 51 | float distance = (totalTime * 0.034)/2; 52 | 53 | cout << "Distance is: " << distance << " cm"< 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | // Definining device parameters 9 | #define I2C_DEVICE_ADDRESS 0x27 // I2C device address 10 | #define firstrow 0x80 // 1st line 11 | #define secondrow 0xC0 // 2nd line 12 | 13 | // Define some device constants 14 | #define LCD_CHR 1 // Mode - Sending data 15 | #define LCD_CMD 0 // Mode - Sending command 16 | 17 | #define LCD_BACKLIGHT 0x08 18 | 19 | #define ENABLE 0b00000100 // Enable bit 20 | 21 | #define trigger 12 22 | #define echo 13 23 | 24 | long startTime; 25 | long stopTime; 26 | 27 | 28 | void init_lcd(void); 29 | void lcd_byte(int bits, int mode); 30 | void lcd_toggle_enable(int bits); 31 | void printmessage(const char *s); 32 | void moveCursor(int line); 33 | void clear(void); 34 | void typeChar(char val); 35 | void printInt(int i); 36 | void printFloat(float myFloat); 37 | int lcdaddr; 38 | 39 | int main() { 40 | wiringPiSetup(); 41 | lcdaddr = wiringPiI2CSetup(I2C_DEVICE_ADDRESS); 42 | init_lcd(); //initializing or setting up the LCD 43 | 44 | pinMode(trigger,OUTPUT); 45 | pinMode(echo, INPUT); 46 | pinMode(0,OUTPUT); 47 | pinMode(2,OUTPUT); 48 | pinMode(3,OUTPUT); 49 | pinMode(4,OUTPUT); 50 | 51 | for(;;) { 52 | 53 | digitalWrite(trigger,LOW); 54 | delay(500); 55 | 56 | digitalWrite(trigger,HIGH); 57 | delayMicroseconds(10); 58 | 59 | digitalWrite(trigger,LOW); 60 | 61 | while(digitalRead(echo) == LOW); 62 | startTime = micros(); 63 | 64 | while(digitalRead(echo) == HIGH); 65 | stopTime = micros(); 66 | 67 | long totalTime= stopTime - startTime; 68 | float distance = (totalTime * 0.034)/2; 69 | 70 | //cout << "Distance is: " << distance << " cm"< 2 | #include 3 | 4 | int main() 5 | { 6 | initscr(); //initializes and clear the screen 7 | int keypressed = getch() 8 | if(keypressed == 'h' || keypressed == 'H') 9 | { 10 | printw("Hello World"); //will print Hello World message 11 | } 12 | getch(); 13 | refresh(); 14 | 15 | endwin(); // frees up memory and ends ncurses 16 | return 0; 17 | } -------------------------------------------------------------------------------- /Chapter05/LEDBuzzer.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | int main() 5 | { 6 | wiringPiSetup(); 7 | 8 | pinMode(15,OUTPUT); //LED 1 pin 9 | pinMode(4, OUTPUT); //LED 2 pin 10 | pinMode(27,OUTPUT); //Buzzer pin 11 | 12 | for(;;){ 13 | 14 | initscr(); 15 | 16 | int keypressed = getch(); 17 | 18 | if(keypressed=='L' || keypressed=='l') 19 | { 20 | digitalWrite(15,HIGH); 21 | delay(1000); 22 | digitalWrite(15,LOW); 23 | delay(1000); 24 | } 25 | 26 | if(keypressed== 69 || keypressed=='e') // 69 is ASCII number for E. 27 | { 28 | digitalWrite(4,HIGH); 29 | delay(1000); 30 | digitalWrite(4,LOW); 31 | delay(1000); 32 | } 33 | 34 | if(keypressed=='D' || keypressed=='d') 35 | { 36 | digitalWrite(15,HIGH); 37 | delay(1000); 38 | digitalWrite(15,LOW); 39 | delay(1000); 40 | digitalWrite(4,HIGH); 41 | delay(1000); 42 | digitalWrite(4,LOW); 43 | delay(1000); 44 | } 45 | 46 | if(keypressed=='B' || keypressed== 98) //98 is ASCII number for b 47 | { 48 | digitalWrite(27,HIGH); 49 | delay(1000); 50 | digitalWrite(27,LOW); 51 | delay(1000); 52 | digitalWrite(27,HIGH); 53 | delay(1000); 54 | digitalWrite(27,LOW); 55 | delay(1000); 56 | } 57 | 58 | if(keypressed=='x' || keypressed =='X') 59 | { 60 | break; 61 | } 62 | 63 | refresh(); 64 | } 65 | endwin(); // 66 | return 0; 67 | } -------------------------------------------------------------------------------- /Chapter05/LEDOnOff/LEDOnOff.pro: -------------------------------------------------------------------------------- 1 | #------------------------------------------------- 2 | # 3 | # Project created by QtCreator 2019-03-28T08:27:17 4 | # 5 | #------------------------------------------------- 6 | 7 | QT += core gui 8 | 9 | greaterThan(QT_MAJOR_VERSION, 4): QT += widgets 10 | 11 | TARGET = LEDOnOff 12 | TEMPLATE = app 13 | 14 | # The following define makes your compiler emit warnings if you use 15 | # any feature of Qt which as been marked as deprecated (the exact warnings 16 | # depend on your compiler). Please consult the documentation of the 17 | # deprecated API in order to know how to port your code away from it. 18 | DEFINES += QT_DEPRECATED_WARNINGS 19 | 20 | # You can also make your code fail to compile if you use deprecated APIs. 21 | # In order to do so, uncomment the following line. 22 | # You can also select to disable deprecated APIs only up to a certain version of Qt. 23 | #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 24 | 25 | 26 | SOURCES += main.cpp\ 27 | mainwindow.cpp 28 | 29 | HEADERS += mainwindow.h 30 | 31 | FORMS += mainwindow.ui 32 | 33 | LIBS += -L/usr/local/lib -lwiringPi 34 | -------------------------------------------------------------------------------- /Chapter05/LEDOnOff/LEDOnOff.pro.user: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | EnvironmentId 7 | {80878e64-9d89-4f4b-ab1c-cf482c7746d8} 8 | 9 | 10 | ProjectExplorer.Project.ActiveTarget 11 | 0 12 | 13 | 14 | ProjectExplorer.Project.EditorSettings 15 | 16 | true 17 | false 18 | true 19 | 20 | Cpp 21 | 22 | CppGlobal 23 | 24 | 25 | 26 | QmlJS 27 | 28 | QmlJSGlobal 29 | 30 | 31 | 2 32 | UTF-8 33 | false 34 | 4 35 | false 36 | 80 37 | true 38 | true 39 | 1 40 | true 41 | false 42 | 0 43 | true 44 | true 45 | 0 46 | 8 47 | true 48 | 1 49 | true 50 | true 51 | true 52 | false 53 | 54 | 55 | 56 | ProjectExplorer.Project.PluginSettings 57 | 58 | 59 | 60 | ProjectExplorer.Project.Target.0 61 | 62 | Desktop 63 | Desktop 64 | {99021ba4-c59c-4a77-b718-60e2680968e7} 65 | 1 66 | 0 67 | 0 68 | 69 | /home/pi/QTPrograms/build-LEDOnOff-RPi-Debug 70 | 71 | 72 | true 73 | qmake 74 | 75 | QtProjectManager.QMakeBuildStep 76 | true 77 | 78 | false 79 | false 80 | false 81 | 82 | 83 | true 84 | Make 85 | 86 | Qt4ProjectManager.MakeStep 87 | 88 | -w 89 | -r 90 | 91 | false 92 | 93 | 94 | 95 | 2 96 | Build 97 | 98 | ProjectExplorer.BuildSteps.Build 99 | 100 | 101 | 102 | true 103 | Make 104 | 105 | Qt4ProjectManager.MakeStep 106 | 107 | -w 108 | -r 109 | 110 | true 111 | clean 112 | 113 | 114 | 1 115 | Clean 116 | 117 | ProjectExplorer.BuildSteps.Clean 118 | 119 | 2 120 | false 121 | 122 | Debug 123 | 124 | Qt4ProjectManager.Qt4BuildConfiguration 125 | 2 126 | true 127 | 128 | 129 | /home/pi/QTPrograms/build-LEDOnOff-RPi-Release 130 | 131 | 132 | true 133 | qmake 134 | 135 | QtProjectManager.QMakeBuildStep 136 | false 137 | 138 | false 139 | false 140 | false 141 | 142 | 143 | true 144 | Make 145 | 146 | Qt4ProjectManager.MakeStep 147 | 148 | -w 149 | -r 150 | 151 | false 152 | 153 | 154 | 155 | 2 156 | Build 157 | 158 | ProjectExplorer.BuildSteps.Build 159 | 160 | 161 | 162 | true 163 | Make 164 | 165 | Qt4ProjectManager.MakeStep 166 | 167 | -w 168 | -r 169 | 170 | true 171 | clean 172 | 173 | 174 | 1 175 | Clean 176 | 177 | ProjectExplorer.BuildSteps.Clean 178 | 179 | 2 180 | false 181 | 182 | Release 183 | 184 | Qt4ProjectManager.Qt4BuildConfiguration 185 | 0 186 | true 187 | 188 | 189 | /home/pi/QTPrograms/build-LEDOnOff-RPi-Profile 190 | 191 | 192 | true 193 | qmake 194 | 195 | QtProjectManager.QMakeBuildStep 196 | true 197 | 198 | false 199 | true 200 | false 201 | 202 | 203 | true 204 | Make 205 | 206 | Qt4ProjectManager.MakeStep 207 | 208 | -w 209 | -r 210 | 211 | false 212 | 213 | 214 | 215 | 2 216 | Build 217 | 218 | ProjectExplorer.BuildSteps.Build 219 | 220 | 221 | 222 | true 223 | Make 224 | 225 | Qt4ProjectManager.MakeStep 226 | 227 | -w 228 | -r 229 | 230 | true 231 | clean 232 | 233 | 234 | 1 235 | Clean 236 | 237 | ProjectExplorer.BuildSteps.Clean 238 | 239 | 2 240 | false 241 | 242 | Profile 243 | 244 | Qt4ProjectManager.Qt4BuildConfiguration 245 | 0 246 | true 247 | 248 | 3 249 | 250 | 251 | 0 252 | Deploy 253 | 254 | ProjectExplorer.BuildSteps.Deploy 255 | 256 | 1 257 | Deploy locally 258 | 259 | ProjectExplorer.DefaultDeployConfiguration 260 | 261 | 1 262 | 263 | 264 | false 265 | false 266 | 1000 267 | 268 | true 269 | 270 | false 271 | false 272 | false 273 | false 274 | true 275 | 0.01 276 | 10 277 | true 278 | 1 279 | 25 280 | 281 | 1 282 | true 283 | false 284 | true 285 | valgrind 286 | 287 | 0 288 | 1 289 | 2 290 | 3 291 | 4 292 | 5 293 | 6 294 | 7 295 | 8 296 | 9 297 | 10 298 | 11 299 | 12 300 | 13 301 | 14 302 | 303 | 2 304 | 305 | LEDOnOff 306 | 307 | Qt4ProjectManager.Qt4RunConfiguration:/home/pi/QTPrograms/LEDOnOff/LEDOnOff.pro 308 | true 309 | 310 | LEDOnOff.pro 311 | false 312 | 313 | 314 | 3768 315 | false 316 | true 317 | false 318 | false 319 | true 320 | 321 | 1 322 | 323 | 324 | 325 | ProjectExplorer.Project.TargetCount 326 | 1 327 | 328 | 329 | ProjectExplorer.Project.Updater.FileVersion 330 | 18 331 | 332 | 333 | Version 334 | 18 335 | 336 | 337 | -------------------------------------------------------------------------------- /Chapter05/LEDOnOff/main.cpp: -------------------------------------------------------------------------------- 1 | #include "mainwindow.h" 2 | #include 3 | 4 | int main(int argc, char *argv[]) 5 | { 6 | QApplication a(argc, argv); 7 | MainWindow w; 8 | w.show(); 9 | 10 | return a.exec(); 11 | } 12 | -------------------------------------------------------------------------------- /Chapter05/LEDOnOff/mainwindow.cpp: -------------------------------------------------------------------------------- 1 | #include "mainwindow.h" 2 | #include "ui_mainwindow.h" 3 | 4 | MainWindow::MainWindow(QWidget *parent) : 5 | QMainWindow(parent), 6 | ui(new Ui::MainWindow) 7 | { 8 | ui->setupUi(this); 9 | wiringPiSetup(); 10 | pinMode(led1, OUTPUT); 11 | pinMode(led2, OUTPUT); 12 | } 13 | 14 | MainWindow::~MainWindow() 15 | { 16 | delete ui; 17 | } 18 | 19 | void MainWindow::on_on_clicked() 20 | { 21 | digitalWrite(led1,HIGH); 22 | digitalWrite(led2,HIGH); 23 | } 24 | 25 | void MainWindow::on_off_clicked() 26 | { 27 | digitalWrite(led1,LOW); 28 | digitalWrite(led2,LOW); 29 | } 30 | 31 | void MainWindow::on_onoff_pressed() 32 | { 33 | digitalWrite(led1,HIGH); 34 | digitalWrite(led2,HIGH); 35 | } 36 | 37 | void MainWindow::on_onoff_released() 38 | { 39 | digitalWrite(led1,LOW); 40 | digitalWrite(led2,LOW); 41 | } 42 | -------------------------------------------------------------------------------- /Chapter05/LEDOnOff/mainwindow.h: -------------------------------------------------------------------------------- 1 | #ifndef MAINWINDOW_H 2 | #define MAINWINDOW_H 3 | #include 4 | #include 5 | #define led1 0 6 | #define led2 2 7 | 8 | #include 9 | 10 | namespace Ui { 11 | class MainWindow; 12 | } 13 | 14 | class MainWindow : public QMainWindow 15 | { 16 | Q_OBJECT 17 | 18 | public: 19 | explicit MainWindow(QWidget *parent = 0); 20 | ~MainWindow(); 21 | 22 | private slots: 23 | void on_on_clicked(); 24 | 25 | void on_off_clicked(); 26 | 27 | void on_onoff_pressed(); 28 | 29 | void on_onoff_released(); 30 | 31 | private: 32 | Ui::MainWindow *ui; 33 | }; 34 | 35 | #endif // MAINWINDOW_H 36 | -------------------------------------------------------------------------------- /Chapter05/LEDOnOff/mainwindow.ui: -------------------------------------------------------------------------------- 1 | 2 | 3 | MainWindow 4 | 5 | 6 | 7 | 0 8 | 0 9 | 400 10 | 300 11 | 12 | 13 | 14 | MainWindow 15 | 16 | 17 | 18 | 19 | 20 | 20 21 | 70 22 | 101 23 | 31 24 | 25 | 26 | 27 | ON 28 | 29 | 30 | 31 | 32 | 33 | 240 34 | 70 35 | 101 36 | 31 37 | 38 | 39 | 40 | OFF 41 | 42 | 43 | 44 | 45 | 46 | 140 47 | 170 48 | 101 49 | 31 50 | 51 | 52 | 53 | ON/OFF 54 | 55 | 56 | 57 | 58 | 59 | 60 | 0 61 | 0 62 | 400 63 | 27 64 | 65 | 66 | 67 | 68 | 69 | TopToolBarArea 70 | 71 | 72 | false 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | -------------------------------------------------------------------------------- /Chapter05/Laptop_Controlled_Rover.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | 6 | void forward() 7 | { 8 | digitalWrite(0,HIGH); 9 | digitalWrite(2,LOW); 10 | digitalWrite(3,HIGH); 11 | digitalWrite(4,LOW); 12 | } 13 | 14 | void rightturn() 15 | { 16 | digitalWrite(0,HIGH); 17 | digitalWrite(2,LOW); 18 | digitalWrite(3,LOW); 19 | digitalWrite(4,HIGH); 20 | } 21 | 22 | void stop() 23 | { 24 | digitalWrite(0,HIGH); 25 | digitalWrite(2,HIGH); 26 | digitalWrite(3,HIGH); 27 | digitalWrite(4,HIGH); 28 | } 29 | 30 | 31 | int main() 32 | { 33 | wiringPiSetup(); 34 | pinMode(0, OUTPUT); 35 | pinMode(2, OUTPUT); 36 | pinMode(3, OUTPUT); 37 | pinMode(4, OUTPUT); 38 | for(;;) 39 | { 40 | initscr(); 41 | keypad(stdscr,TRUE); 42 | refresh(); 43 | int keypressed = getch(); 44 | if(keypressed==KEY_UP || keypressed == 'W' || keypressed == 'w') 45 | { 46 | digitalWrite(0,HIGH); 47 | digitalWrite(2,LOW); 48 | digitalWrite(3,HIGH); 49 | digitalWrite(4,LOW); 50 | } 51 | if(keypressed==KEY_DOWN || keypressed == 'X' || keypressed == 'x') 52 | { 53 | digitalWrite(0,LOW); 54 | digitalWrite(2,HIGH); 55 | digitalWrite(3,LOW); 56 | digitalWrite(4,HIGH); 57 | } 58 | if(keypressed==KEY_LEFT || keypressed == 'A' || keypressed == 'a') 59 | { 60 | digitalWrite(0,LOW); 61 | digitalWrite(2,HIGH); 62 | digitalWrite(3,HIGH); 63 | digitalWrite(4,LOW); 64 | } 65 | if(keypressed==KEY_RIGHT || keypressed == 'D' || keypressed == 'd') 66 | { 67 | digitalWrite(0,HIGH); 68 | digitalWrite(2,LOW); 69 | digitalWrite(3,LOW); 70 | digitalWrite(4,HIGH); 71 | } 72 | if(keypressed=='s' || keypressed=='S') 73 | { 74 | digitalWrite(0,HIGH); 75 | digitalWrite(2,HIGH); 76 | digitalWrite(3,HIGH); 77 | digitalWrite(4,HIGH); 78 | } 79 | if(keypressed == 'r' || keypressed == 'R') 80 | { 81 | forward(); //first forward movement 82 | delay(2000); 83 | rightturn(); //first left turn 84 | delay(500); //delay needs to be such that the robot takes a perfect 90 degree right turn 85 | 86 | forward(); //second forward movement 87 | delay(2000); 88 | rightturn(); //second right turn 89 | delay(500); 90 | 91 | forward(); //third forward movement 92 | delay(2000); 93 | rightturn(); //third and last left turn 94 | delay(500); 95 | 96 | forward(); //fourth and last forward movement 97 | delay(2000); 98 | stop(); //stop condition 99 | } 100 | if(keypressed=='e' || keypressed=='E') 101 | { 102 | break; 103 | } 104 | } 105 | endwin(); 106 | return 0; 107 | } 108 | -------------------------------------------------------------------------------- /Chapter05/QTRover/QTRover.pro: -------------------------------------------------------------------------------- 1 | #------------------------------------------------- 2 | # 3 | # Project created by QtCreator 2019-03-28T16:36:35 4 | # 5 | #------------------------------------------------- 6 | 7 | QT += core gui 8 | 9 | greaterThan(QT_MAJOR_VERSION, 4): QT += widgets 10 | 11 | TARGET = QTRover 12 | TEMPLATE = app 13 | 14 | # The following define makes your compiler emit warnings if you use 15 | # any feature of Qt which as been marked as deprecated (the exact warnings 16 | # depend on your compiler). Please consult the documentation of the 17 | # deprecated API in order to know how to port your code away from it. 18 | DEFINES += QT_DEPRECATED_WARNINGS 19 | 20 | # You can also make your code fail to compile if you use deprecated APIs. 21 | # In order to do so, uncomment the following line. 22 | # You can also select to disable deprecated APIs only up to a certain version of Qt. 23 | #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 24 | 25 | 26 | SOURCES += main.cpp\ 27 | mainwindow.cpp 28 | 29 | HEADERS += mainwindow.h 30 | 31 | FORMS += mainwindow.ui 32 | 33 | LIBS += -L/usr/local/lib -lwiringPi 34 | -------------------------------------------------------------------------------- /Chapter05/QTRover/QTRover.pro.user: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | EnvironmentId 7 | {80878e64-9d89-4f4b-ab1c-cf482c7746d8} 8 | 9 | 10 | ProjectExplorer.Project.ActiveTarget 11 | 0 12 | 13 | 14 | ProjectExplorer.Project.EditorSettings 15 | 16 | true 17 | false 18 | true 19 | 20 | Cpp 21 | 22 | CppGlobal 23 | 24 | 25 | 26 | QmlJS 27 | 28 | QmlJSGlobal 29 | 30 | 31 | 2 32 | UTF-8 33 | false 34 | 4 35 | false 36 | 80 37 | true 38 | true 39 | 1 40 | true 41 | false 42 | 0 43 | true 44 | true 45 | 0 46 | 8 47 | true 48 | 1 49 | true 50 | true 51 | true 52 | false 53 | 54 | 55 | 56 | ProjectExplorer.Project.PluginSettings 57 | 58 | 59 | 60 | ProjectExplorer.Project.Target.0 61 | 62 | Desktop 63 | Desktop 64 | {99021ba4-c59c-4a77-b718-60e2680968e7} 65 | 0 66 | 0 67 | 0 68 | 69 | /home/pi/build-QTRover-RPi-Debug 70 | 71 | 72 | true 73 | qmake 74 | 75 | QtProjectManager.QMakeBuildStep 76 | true 77 | 78 | false 79 | false 80 | false 81 | 82 | 83 | true 84 | Make 85 | 86 | Qt4ProjectManager.MakeStep 87 | 88 | -w 89 | -r 90 | 91 | false 92 | 93 | 94 | 95 | 2 96 | Build 97 | 98 | ProjectExplorer.BuildSteps.Build 99 | 100 | 101 | 102 | true 103 | Make 104 | 105 | Qt4ProjectManager.MakeStep 106 | 107 | -w 108 | -r 109 | 110 | true 111 | clean 112 | 113 | 114 | 1 115 | Clean 116 | 117 | ProjectExplorer.BuildSteps.Clean 118 | 119 | 2 120 | false 121 | 122 | Debug 123 | 124 | Qt4ProjectManager.Qt4BuildConfiguration 125 | 2 126 | true 127 | 128 | 129 | /home/pi/build-QTRover-RPi-Release 130 | 131 | 132 | true 133 | qmake 134 | 135 | QtProjectManager.QMakeBuildStep 136 | false 137 | 138 | false 139 | false 140 | false 141 | 142 | 143 | true 144 | Make 145 | 146 | Qt4ProjectManager.MakeStep 147 | 148 | -w 149 | -r 150 | 151 | false 152 | 153 | 154 | 155 | 2 156 | Build 157 | 158 | ProjectExplorer.BuildSteps.Build 159 | 160 | 161 | 162 | true 163 | Make 164 | 165 | Qt4ProjectManager.MakeStep 166 | 167 | -w 168 | -r 169 | 170 | true 171 | clean 172 | 173 | 174 | 1 175 | Clean 176 | 177 | ProjectExplorer.BuildSteps.Clean 178 | 179 | 2 180 | false 181 | 182 | Release 183 | 184 | Qt4ProjectManager.Qt4BuildConfiguration 185 | 0 186 | true 187 | 188 | 189 | /home/pi/build-QTRover-RPi-Profile 190 | 191 | 192 | true 193 | qmake 194 | 195 | QtProjectManager.QMakeBuildStep 196 | true 197 | 198 | false 199 | true 200 | false 201 | 202 | 203 | true 204 | Make 205 | 206 | Qt4ProjectManager.MakeStep 207 | 208 | -w 209 | -r 210 | 211 | false 212 | 213 | 214 | 215 | 2 216 | Build 217 | 218 | ProjectExplorer.BuildSteps.Build 219 | 220 | 221 | 222 | true 223 | Make 224 | 225 | Qt4ProjectManager.MakeStep 226 | 227 | -w 228 | -r 229 | 230 | true 231 | clean 232 | 233 | 234 | 1 235 | Clean 236 | 237 | ProjectExplorer.BuildSteps.Clean 238 | 239 | 2 240 | false 241 | 242 | Profile 243 | 244 | Qt4ProjectManager.Qt4BuildConfiguration 245 | 0 246 | true 247 | 248 | 3 249 | 250 | 251 | 0 252 | Deploy 253 | 254 | ProjectExplorer.BuildSteps.Deploy 255 | 256 | 1 257 | Deploy locally 258 | 259 | ProjectExplorer.DefaultDeployConfiguration 260 | 261 | 1 262 | 263 | 264 | false 265 | false 266 | 1000 267 | 268 | true 269 | 270 | false 271 | false 272 | false 273 | false 274 | true 275 | 0.01 276 | 10 277 | true 278 | 1 279 | 25 280 | 281 | 1 282 | true 283 | false 284 | true 285 | valgrind 286 | 287 | 0 288 | 1 289 | 2 290 | 3 291 | 4 292 | 5 293 | 6 294 | 7 295 | 8 296 | 9 297 | 10 298 | 11 299 | 12 300 | 13 301 | 14 302 | 303 | 2 304 | 305 | QTRover 306 | 307 | Qt4ProjectManager.Qt4RunConfiguration:/home/pi/QTRover/QTRover.pro 308 | true 309 | 310 | QTRover.pro 311 | false 312 | 313 | 314 | 3768 315 | false 316 | true 317 | false 318 | false 319 | true 320 | 321 | 1 322 | 323 | 324 | 325 | ProjectExplorer.Project.TargetCount 326 | 1 327 | 328 | 329 | ProjectExplorer.Project.Updater.FileVersion 330 | 18 331 | 332 | 333 | Version 334 | 18 335 | 336 | 337 | -------------------------------------------------------------------------------- /Chapter05/QTRover/main.cpp: -------------------------------------------------------------------------------- 1 | #include "mainwindow.h" 2 | #include 3 | 4 | int main(int argc, char *argv[]) 5 | { 6 | QApplication a(argc, argv); 7 | MainWindow w; 8 | w.show(); 9 | 10 | return a.exec(); 11 | } 12 | -------------------------------------------------------------------------------- /Chapter05/QTRover/mainwindow.cpp: -------------------------------------------------------------------------------- 1 | #include "mainwindow.h" 2 | #include "ui_mainwindow.h" 3 | 4 | MainWindow::MainWindow(QWidget *parent) : 5 | QMainWindow(parent), 6 | ui(new Ui::MainWindow) 7 | { 8 | ui->setupUi(this); 9 | wiringPiSetup(); 10 | pinMode(leftmotor1, OUTPUT); 11 | pinMode(leftmotor2, OUTPUT); 12 | pinMode(rightmotor1,OUTPUT); 13 | pinMode(rightmotor2,OUTPUT); 14 | 15 | } 16 | 17 | MainWindow::~MainWindow() 18 | { 19 | delete ui; 20 | } 21 | 22 | void MainWindow::on_forward_clicked() 23 | { 24 | digitalWrite(leftmotor1,HIGH); 25 | digitalWrite(leftmotor2,LOW); 26 | digitalWrite(rightmotor1,HIGH); 27 | digitalWrite(rightmotor2,LOW); 28 | } 29 | 30 | void MainWindow::on_backward_clicked() 31 | { 32 | digitalWrite(leftmotor1,LOW); 33 | digitalWrite(leftmotor2,HIGH); 34 | digitalWrite(rightmotor1,LOW); 35 | digitalWrite(rightmotor2,HIGH); 36 | } 37 | 38 | void MainWindow::on_stop_clicked() 39 | { 40 | digitalWrite(leftmotor1,HIGH); 41 | digitalWrite(leftmotor2,HIGH); 42 | digitalWrite(rightmotor1,HIGH); 43 | digitalWrite(rightmotor2,HIGH); 44 | } 45 | 46 | void MainWindow::on_left_pressed() 47 | { 48 | digitalWrite(leftmotor1,LOW); 49 | digitalWrite(leftmotor2,HIGH); 50 | digitalWrite(rightmotor1,HIGH); 51 | digitalWrite(rightmotor2,LOW); 52 | } 53 | 54 | void MainWindow::on_left_released() 55 | { 56 | digitalWrite(leftmotor1,HIGH); 57 | digitalWrite(leftmotor2,HIGH); 58 | digitalWrite(rightmotor1,HIGH); 59 | digitalWrite(rightmotor2,HIGH); 60 | } 61 | 62 | void MainWindow::on_right_pressed() 63 | { 64 | digitalWrite(leftmotor1,HIGH); 65 | digitalWrite(leftmotor2,LOW); 66 | digitalWrite(rightmotor1,LOW); 67 | digitalWrite(rightmotor2,HIGH); 68 | } 69 | 70 | void MainWindow::on_right_released() 71 | { 72 | digitalWrite(leftmotor1,HIGH); 73 | digitalWrite(leftmotor2,HIGH); 74 | digitalWrite(rightmotor1,HIGH); 75 | digitalWrite(rightmotor2,HIGH); 76 | } 77 | -------------------------------------------------------------------------------- /Chapter05/QTRover/mainwindow.h: -------------------------------------------------------------------------------- 1 | #ifndef MAINWINDOW_H 2 | #define MAINWINDOW_H 3 | #include 4 | #include 5 | 6 | #define leftmotor1 0 7 | #define leftmotor2 2 8 | #define rightmotor1 3 9 | #define rightmotor2 4 10 | namespace Ui { 11 | class MainWindow; 12 | } 13 | 14 | class MainWindow : public QMainWindow 15 | { 16 | Q_OBJECT 17 | 18 | public: 19 | explicit MainWindow(QWidget *parent = 0); 20 | ~MainWindow(); 21 | 22 | private slots: 23 | void on_forward_clicked(); 24 | 25 | void on_backward_clicked(); 26 | 27 | void on_stop_clicked(); 28 | 29 | void on_left_pressed(); 30 | 31 | void on_left_released(); 32 | 33 | void on_right_pressed(); 34 | 35 | void on_right_released(); 36 | 37 | private: 38 | Ui::MainWindow *ui; 39 | }; 40 | 41 | #endif // MAINWINDOW_H 42 | -------------------------------------------------------------------------------- /Chapter05/QTRover/mainwindow.ui: -------------------------------------------------------------------------------- 1 | 2 | 3 | MainWindow 4 | 5 | 6 | 7 | 0 8 | 0 9 | 400 10 | 300 11 | 12 | 13 | 14 | MainWindow 15 | 16 | 17 | 18 | 19 | 20 | 130 21 | -10 22 | 101 23 | 31 24 | 25 | 26 | 27 | FORWARD 28 | 29 | 30 | 31 | 32 | 33 | 140 34 | 90 35 | 101 36 | 31 37 | 38 | 39 | 40 | STOP 41 | 42 | 43 | 44 | 45 | 46 | 270 47 | 90 48 | 101 49 | 31 50 | 51 | 52 | 53 | RIGHT 54 | 55 | 56 | 57 | 58 | 59 | 10 60 | 90 61 | 101 62 | 31 63 | 64 | 65 | 66 | LEFT 67 | 68 | 69 | 70 | 71 | 72 | 140 73 | 190 74 | 101 75 | 31 76 | 77 | 78 | 79 | BACKWARD 80 | 81 | 82 | 83 | 84 | 85 | 86 | 0 87 | 0 88 | 400 89 | 27 90 | 91 | 92 | 93 | 94 | 95 | TopToolBarArea 96 | 97 | 98 | false 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | -------------------------------------------------------------------------------- /Chapter06/DisplayImage.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace cv; 6 | using namespace std; 7 | int main() 8 | { 9 | 10 | Mat img; 11 | 12 | img = imread("Car.jpg"); 13 | 14 | imshow("Car Image", img); 15 | 16 | waitKey(0); 17 | 18 | return 0; 19 | } -------------------------------------------------------------------------------- /Chapter07/BallTracing.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace cv; 7 | using namespace std; 8 | int main() 9 | { 10 | Mat videofeed,resizevideo,thresholdvideo; 11 | VideoCapture vid(0); 12 | if (!vid.isOpened()) 13 | { 14 | return -1; 15 | } 16 | for (;;) 17 | { 18 | vid.read(videofeed); 19 | resize(videofeed, resizevideo, cvSize(640, 480)); 20 | flip(resizevideo, resizevideo, 1); 21 | 22 | inRange(resizevideo, Scalar(39, 140, 34), Scalar(122, 245, 119), thresholdvideo); 23 | 24 | Moments m = moments(thresholdvideo,true); 25 | int x,y; 26 | x = m.m10/m.m00; 27 | y = m.m01/m.m00; 28 | Point p(x,y); 29 | 30 | circle(resizevideo, p, 10, Scalar(0,0,128), -1); 31 | 32 | imshow("Image with center",resizevideo); 33 | imshow("Thresolding Video",thresholdvideo); 34 | 35 | cout<= 0) break; 38 | } 39 | return 0; 40 | } 41 | 42 | 43 | -------------------------------------------------------------------------------- /Chapter07/Camerafeed.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | using namespace cv; 8 | using namespace std; 9 | int main() 10 | { Mat videoframe; 11 | VideoCapture vid(0); 12 | if (!vid.isOpened()) 13 | { 14 | cout<<"Error opening camera"< 0) break; 22 | } 23 | return 0; 24 | } 25 | 26 | -------------------------------------------------------------------------------- /Chapter07/ObjectDetection.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace cv; 7 | using namespace std; 8 | int main() 9 | { 10 | Mat img, resizeimg,thresimage; 11 | img = imread("greenball.png"); 12 | imshow("Green Ball Image", img); 13 | waitKey(0); 14 | resize(img, resizeimg, cvSize(640, 480)); 15 | imshow("Resized Image", resizeimg); 16 | waitKey(0); 17 | inRange(resizeimg, Scalar(39, 140, 34), Scalar(122, 245, 119), 18 | thresimage); 19 | imshow("Thresholded Image", thresimage); 20 | waitKey(0); 21 | Moments m = moments(thresimage,true); 22 | int x,y; 23 | x = m.m10/m.m00; 24 | y = m.m01/m.m00; 25 | Point p(x,y); 26 | circle(img, p, 5, Scalar(0,0,200), -1); 27 | imshow("Image with center",img); 28 | waitKey(0); 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /Chapter07/ObjectFollowingRobot.cpp: -------------------------------------------------------------------------------- 1 | /*This programs will lag a little bit as the ultrasonic sensor will try to measure 2 | * distance and the camera will try tracking the colored ball. You can also check 3 | * the ObjectFollowingRobotwithoutUltrasonic.cpp program in which the Ultrasonic sensor is 4 | * completely removed and because of this the ball tracking is done faster in that program */ 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | using namespace cv; 13 | using namespace std; 14 | 15 | #define trigger 12 16 | #define echo 13 17 | 18 | long startTime; 19 | long stopTime; 20 | 21 | void forward() 22 | { 23 | digitalWrite(0,HIGH); 24 | digitalWrite(2,LOW); 25 | digitalWrite(3,HIGH); 26 | digitalWrite(4,LOW); 27 | } 28 | 29 | /*In left function, program for taking a right turn is written as your right side is the left side for the robot*/ 30 | void left() 31 | { 32 | digitalWrite(0,HIGH); 33 | digitalWrite(2,LOW); 34 | digitalWrite(3,LOW); 35 | digitalWrite(4,HIGH); 36 | } 37 | 38 | /*In right function, program for taking a left turn is written as your left side is the right side for the robot*/ 39 | void right() 40 | { 41 | digitalWrite(0,LOW); 42 | digitalWrite(2,HIGH); 43 | digitalWrite(3,HIGH); 44 | digitalWrite(4,LOW); 45 | } 46 | 47 | void stop() 48 | { 49 | digitalWrite(0,HIGH); 50 | digitalWrite(2,HIGH); 51 | digitalWrite(3,HIGH); 52 | digitalWrite(4,HIGH); 53 | } 54 | 55 | int main() 56 | { 57 | wiringPiSetup(); 58 | 59 | pinMode(trigger,OUTPUT); 60 | pinMode(echo, INPUT); 61 | 62 | pinMode(0,OUTPUT); 63 | pinMode(2,OUTPUT); 64 | pinMode(3,OUTPUT); 65 | pinMode(4,OUTPUT); 66 | 67 | Mat videofeed,resizevideo,thresholdvideo; 68 | 69 | VideoCapture vid(0); 70 | 71 | if (!vid.isOpened()) 72 | { 73 | return -1; 74 | } 75 | for (;;) 76 | { 77 | vid.read(videofeed); 78 | resize(videofeed, resizevideo, cvSize(640, 480)); 79 | flip(resizevideo, resizevideo, 0); 80 | 81 | inRange(resizevideo, Scalar(39, 140, 34), Scalar(122, 245, 119), thresholdvideo); 82 | 83 | Moments m = moments(thresholdvideo,true); 84 | int x,y; 85 | x = m.m10/m.m00; 86 | y = m.m01/m.m00; 87 | Point p(x,y); 88 | 89 | circle(resizevideo, p, 10, Scalar(0,0,128), -1); 90 | 91 | imshow("Image with center",resizevideo); 92 | imshow("Thresolding Video",thresholdvideo); 93 | 94 | digitalWrite(trigger,LOW); 95 | delay(500); 96 | 97 | digitalWrite(trigger,HIGH); 98 | delayMicroseconds(10); 99 | 100 | digitalWrite(trigger,LOW); 101 | 102 | while(digitalRead(echo) == LOW); 103 | startTime = micros(); 104 | 105 | while(digitalRead(echo) == HIGH); 106 | stopTime = micros(); 107 | 108 | long totalTime= stopTime - startTime; 109 | float distance = (totalTime * 0.034)/2; 110 | 111 | if(distance < 20) 112 | { 113 | cout<<"Object close to Robot"<< " " << Mat(p)<< " " < 20 && x < 170 && y > 20 ) 125 | { 126 | cout<<"LEFT TURN"<< " " << Mat(p)<< " " < 170 && x < 470) 130 | { 131 | cout<<"FORWARD"<< " " << Mat(p)<< " " < 470 && x < 640) 135 | { 136 | cout<<"RIGHT TURN"<< " " << Mat(p)<< " " <= 0) break; 142 | } 143 | return 0; 144 | } 145 | 146 | 147 | 148 | -------------------------------------------------------------------------------- /Chapter07/ObjectFollowingRobotwithoutUltrasonic.cpp: -------------------------------------------------------------------------------- 1 | /*In this program we are not masuring distance using the ultrasonic sensor and thats why this program is faster than ObjectFollowingRobot program*/ 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | using namespace cv; 10 | using namespace std; 11 | 12 | void forward() 13 | { 14 | digitalWrite(0,HIGH); 15 | digitalWrite(2,LOW); 16 | digitalWrite(3,HIGH); 17 | digitalWrite(4,LOW); 18 | } 19 | /*In left function, program for taking a right turn is written as your right side is the left side for the robot*/ 20 | void left() 21 | { 22 | digitalWrite(0,HIGH); 23 | digitalWrite(2,LOW); 24 | digitalWrite(3,LOW); 25 | digitalWrite(4,HIGH); 26 | } 27 | /*In right function, program for taking a left turn is written as your left side is the right side for the robot*/ 28 | void right() 29 | { 30 | digitalWrite(0,LOW); 31 | digitalWrite(2,HIGH); 32 | digitalWrite(3,HIGH); 33 | digitalWrite(4,LOW); 34 | } 35 | 36 | void stop() 37 | { 38 | digitalWrite(0,HIGH); 39 | digitalWrite(2,HIGH); 40 | digitalWrite(3,HIGH); 41 | digitalWrite(4,HIGH); 42 | } 43 | 44 | int main() 45 | { 46 | wiringPiSetup(); 47 | 48 | pinMode(0,OUTPUT); 49 | pinMode(2,OUTPUT); 50 | pinMode(3,OUTPUT); 51 | pinMode(4,OUTPUT); 52 | 53 | Mat videofeed,resizevideo,thresholdvideo; 54 | 55 | VideoCapture vid(0); 56 | 57 | if (!vid.isOpened()) 58 | { 59 | return -1; 60 | } 61 | for (;;) 62 | { 63 | vid.read(videofeed); 64 | resize(videofeed, resizevideo, cvSize(640, 480)); 65 | flip(resizevideo, resizevideo, 0); 66 | 67 | inRange(resizevideo, Scalar(39, 140, 34), Scalar(122, 245, 119), thresholdvideo); 68 | 69 | Moments m = moments(thresholdvideo,true); 70 | int x,y; 71 | x = m.m10/m.m00; 72 | y = m.m01/m.m00; 73 | Point p(x,y); 74 | 75 | circle(resizevideo, p, 10, Scalar(0,0,128), -1); 76 | 77 | imshow("Image with center",resizevideo); 78 | imshow("Thresolding Video",thresholdvideo); 79 | 80 | 81 | if(x<20 && y< 20) 82 | { 83 | cout<<"Object not found"<< Mat(p) << endl; 84 | stop(); 85 | } 86 | 87 | if(x > 20 && x < 170 && y > 20 ) 88 | { 89 | cout<<"LEFT TURN"<< " " << Mat(p) << endl; 90 | left(); 91 | } 92 | 93 | if(x > 170 && x < 470) 94 | { 95 | cout<<"FORWARD"<< " " << Mat(p) << endl; 96 | forward(); 97 | } 98 | 99 | if(x > 470 && x < 640) 100 | { 101 | cout<<"RIGHT TURN"<< " " << Mat(p) << endl; 102 | right(); 103 | } 104 | 105 | if (waitKey(33) >= 0) break; 106 | } 107 | return 0; 108 | } 109 | -------------------------------------------------------------------------------- /Chapter08/FaceDetection.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | using namespace std; 8 | using namespace cv; 9 | 10 | int main(void) 11 | { 12 | CascadeClassifier faceDetector("haarcascade_frontalface_alt2.xml"); 13 | 14 | VideoCapture vid(0); 15 | 16 | Mat videofeed, grayfeed; 17 | 18 | if (!vid.isOpened()) 19 | { 20 | cout<< "Error opening camera"< face; 32 | faceDetector.detectMultiScale(grayfeed, face, 1.1, 5, 0 | CASCADE_FIND_BIGGEST_OBJECT,Size(30, 30)); 33 | 34 | for (size_t f = 0; f < face.size(); f++) 35 | { 36 | rectangle(videofeed, face[f], Scalar(255, 0, 0), 2); 37 | putText(videofeed, "Face Detected", Point(face[f].x, face[f].y), FONT_HERSHEY_PLAIN, 1.1, Scalar(0, 255, 0), 1.0); 38 | } 39 | imshow("Face Detection", videofeed); 40 | 41 | if (waitKey(1) >= 0) break; 42 | 43 | } 44 | return 0; 45 | } 46 | -------------------------------------------------------------------------------- /Chapter08/Facetrackingrobot.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | using namespace std; 10 | using namespace cv; 11 | 12 | 13 | int main(void) 14 | { 15 | wiringPiSetup(); 16 | 17 | pinMode(0,OUTPUT); 18 | pinMode(2,OUTPUT); 19 | pinMode(3,OUTPUT); 20 | 21 | softPwmCreate(24,0,100); 22 | softPwmCreate(27,0,100); 23 | softPwmCreate(25,0,100); 24 | softPwmCreate(28,0,100); 25 | 26 | CascadeClassifier faceDetector("haarcascade_frontalface_alt2.xml"); 27 | CascadeClassifier eyeDetectorright("haarcascade_righteye_2splits.xml"); 28 | CascadeClassifier eyeDetectorleft("haarcascade_lefteye_2splits.xml"); 29 | CascadeClassifier smileDetector("haarcascade_smile.xml"); 30 | 31 | VideoCapture vid(0); 32 | 33 | Mat videofeed, grayfeed; 34 | 35 | if (!vid.isOpened()) 36 | { 37 | cout<< "Error opening camera"< face; 55 | faceDetector.detectMultiScale(grayfeed, face, 1.1, 5, 0 | CASCADE_SCALE_IMAGE,Size(30, 30)); 56 | 57 | for (size_t f = 0; f < face.size(); f++) 58 | { 59 | rectangle(videofeed, face[f], Scalar(255, 0, 0), 2); 60 | putText(videofeed, "Face Detected", Point(face[f].x, face[f].y), FONT_HERSHEY_PLAIN, 1.0, Scalar(0, 255, 0), 1.0); 61 | facex = face[f].x +face[f].width/2; 62 | facey = face[f].y + face[f].height/2; 63 | Point facecenter(facex, facey); 64 | circle(videofeed,facecenter,5,Scalar(255,255,255),-1); 65 | 66 | Mat faceroi = videofeed(face[f]); 67 | vector smile, lefteye, righteye; 68 | 69 | eyeDetectorleft.detectMultiScale(faceroi, lefteye, 1.3, 25, 0 |CASCADE_SCALE_IMAGE,Size(30, 30)); 70 | for (size_t le = 0; le < lefteye.size(); le++) 71 | { 72 | Point center(face[f].x + lefteye[le].x + lefteye[le].width*0.5, face[f].y + lefteye[le].y + lefteye[le].height*0.5); 73 | int radius = cvRound((lefteye[le].width + lefteye[le].height)*0.25); 74 | circle(videofeed, center, radius, Scalar(0, 0, 255), 2); 75 | lefteyedetect = true; 76 | } 77 | 78 | eyeDetectorright.detectMultiScale(faceroi, righteye, 1.3, 25, 0 |CASCADE_SCALE_IMAGE,Size(30, 30)); 79 | for (size_t re = 0; re < righteye.size(); re++) 80 | { 81 | Point center(face[f].x + righteye[re].x + righteye[re].width*0.5, face[f].y + righteye[re].y + righteye[re].height*0.5); 82 | int radius = cvRound((righteye[re].width + righteye[re].height)*0.25); 83 | circle(videofeed, center, radius, Scalar(255, 255, 0), 2); 84 | righteyedetect = true; 85 | } 86 | 87 | smileDetector.detectMultiScale(faceroi, smile, 1.3, 25, 0 |CASCADE_SCALE_IMAGE,Size(30, 30)); 88 | for (size_t sm = 0; sm 0 && facex < 280) 122 | { 123 | /*Now since the camera and the face are looking at each other, so when the white dot moves on the left side 124 | * of the camera the robot should take a right turn in order to follow the white dot*/ 125 | putText(videofeed, "Left", Point(320,10), FONT_HERSHEY_PLAIN, 1.0, CV_RGB(0, 0, 255), 2.0); 126 | softPwmWrite(24, 30); 127 | softPwmWrite(27, 0); 128 | softPwmWrite(25, 0); 129 | softPwmWrite(28, 30); 130 | } 131 | 132 | if(facex > 360 && facex < 640) 133 | { 134 | /*Similarly since the camera and the face are looking at each other, so when the white dot moves on the right side 135 | * of the camera the robot should take a left turn in order to follow the white dot*/ 136 | putText(videofeed, "Right", Point(320,10), FONT_HERSHEY_PLAIN, 1.0, CV_RGB(0, 0, 255), 2.0); 137 | softPwmWrite(24, 0); 138 | softPwmWrite(27, 30); 139 | softPwmWrite(25, 30); 140 | softPwmWrite(28, 0); 141 | } 142 | if(facex > 280 && facex < 360) 143 | { 144 | putText(videofeed, "Middle", Point(320,10), FONT_HERSHEY_PLAIN, 1.0, CV_RGB(0, 0, 255), 2.0); 145 | softPwmWrite(24, 0); 146 | softPwmWrite(27, 0); 147 | softPwmWrite(25, 0); 148 | softPwmWrite(28, 0); 149 | } 150 | imshow("Facial Detection", videofeed); 151 | 152 | if (waitKey(1) >= 0) break; 153 | 154 | } 155 | return 0; 156 | } 157 | -------------------------------------------------------------------------------- /Chapter08/SmilingFace.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | using namespace std; 8 | using namespace cv; 9 | 10 | int main(void) 11 | { 12 | CascadeClassifier faceDetector("haarcascade_frontalface_alt2.xml"); 13 | 14 | CascadeClassifier eyeDetectorright("haarcascade_righteye_2splits.xml"); 15 | 16 | CascadeClassifier eyeDetector("haarcascade_eye.xml"); 17 | 18 | CascadeClassifier eyeDetectorleft("haarcascade_lefteye_2splits.xml"); 19 | 20 | CascadeClassifier smileDetector("haarcascade_smile.xml"); 21 | 22 | 23 | VideoCapture vid(0); 24 | 25 | Mat videofeed, grayfeed; 26 | 27 | if (!vid.isOpened()) 28 | { 29 | cout<< "Error opening camera"< face; 41 | faceDetector.detectMultiScale(grayfeed, face, 1.1, 5, 0 | CASCADE_FIND_BIGGEST_OBJECT,Size(30, 30)); 42 | 43 | for (size_t f = 0; f < face.size(); f++) 44 | { 45 | rectangle(videofeed, face[f], Scalar(255, 0, 0), 2); 46 | putText(videofeed, "Face Detected", Point(face[f].x, face[f].y), FONT_HERSHEY_PLAIN, 1.0, Scalar(0, 255, 0), 1.0); 47 | 48 | Mat faceROI = videofeed(face[f]); 49 | vector eyes, smile, lefteye, righteye; 50 | 51 | eyeDetector.detectMultiScale(faceROI, eyes, 1.3, 5, 0 |CASCADE_SCALE_IMAGE,Size(30, 30)); 52 | for (size_t e = 0; e < eyes.size(); e++) 53 | { 54 | Point center(face[f].x + eyes[e].x + eyes[e].width*0.5, face[f].y + eyes[e].y + eyes[e].height*0.5); 55 | int radius = cvRound((eyes[e].width + eyes[e].height)*0.25); 56 | circle(videofeed, center, radius, Scalar(0, 0, 255), 2, 8, 0); 57 | } 58 | 59 | /* eyeDetectorleft.detectMultiScale(faceROI, lefteye, 1.3, 25, 0 |CASCADE_SCALE_IMAGE,Size(30, 30)); 60 | for (size_t le = 0; le < lefteye.size(); le++) 61 | { 62 | Point center(face[f].x + lefteye[le].x + lefteye[le].width*0.5, face[f].y + lefteye[le].y + lefteye[le].height*0.5); 63 | int radius = cvRound((lefteye[le].width + lefteye[le].height)*0.25); 64 | circle(videofeed, center, radius, Scalar(0, 0, 255), 2, 8, 0); 65 | } 66 | 67 | eyeDetectorright.detectMultiScale(faceROI, righteye, 1.3, 25, 0 |CASCADE_SCALE_IMAGE,Size(30, 30)); 68 | for (size_t re = 0; re < righteye.size(); re++) 69 | { 70 | Point center(face[f].x + righteye[re].x + righteye[re].width*0.5, face[f].y + righteye[re].y + righteye[re].height*0.5); 71 | int radius = cvRound((righteye[re].width + righteye[re].height)*0.25); 72 | circle(videofeed, center, radius, Scalar(0, 255, 0), 2, 8, 0); 73 | }*/ 74 | 75 | smileDetector.detectMultiScale(faceROI, smile, 1.3, 35, 0 |CASCADE_SCALE_IMAGE,Size(30, 30)); 76 | for (size_t sm = 0; sm = 0) break; 86 | 87 | } 88 | return 0; 89 | } 90 | 91 | -------------------------------------------------------------------------------- /Chapter09/Mic.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Hands-On-Robotics-Programming-with-Cpp/ae868642a390c956a3bf2a4ed0817cfad0013f19/Chapter09/Mic.png -------------------------------------------------------------------------------- /Chapter09/TalkingPi.aia: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Hands-On-Robotics-Programming-with-Cpp/ae868642a390c956a3bf2a4ed0817cfad0013f19/Chapter09/TalkingPi.aia -------------------------------------------------------------------------------- /Chapter09/VoiceBot.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | int main(void) 9 | { 10 | wiringPiSetup(); 11 | pinMode(0,OUTPUT); 12 | pinMode(2,OUTPUT); 13 | pinMode(3,OUTPUT); 14 | pinMode(4,OUTPUT); 15 | 16 | struct sockaddr_rc server_address = { 0 }, client_address = { 0 }; 17 | char data[1024] = { 0 }; 18 | int s, clientsocket, bytes; 19 | socklen_t opt = sizeof(client_address); 20 | 21 | 22 | s = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM); 23 | 24 | 25 | server_address.rc_family = AF_BLUETOOTH; 26 | server_address.rc_bdaddr = *BDADDR_ANY; 27 | server_address.rc_channel = (uint8_t) 1; 28 | 29 | bind(s, (struct sockaddr *)&server_address, sizeof(server_address)); 30 | 31 | listen(s, 1); 32 | 33 | 34 | clientsocket = accept(s, (struct sockaddr *)&client_address, &opt); 35 | 36 | ba2str( &client_address.rc_bdaddr, data ); 37 | fprintf(stderr, "Connected to %s\n", data); 38 | memset(data, 0, sizeof(data)); 39 | 40 | for(;;){ 41 | bytes = read(clientsocket, data, sizeof(data)); 42 | if( bytes > 0 ) { 43 | printf("Alphabet: %s\n", data); 44 | if(*data=='F') 45 | { 46 | digitalWrite(0,HIGH); 47 | digitalWrite(2,LOW); 48 | digitalWrite(3,HIGH); 49 | digitalWrite(4,LOW); 50 | } 51 | else if(*data=='B') 52 | { 53 | digitalWrite(0,LOW); 54 | digitalWrite(2,HIGH); 55 | digitalWrite(3,LOW); 56 | digitalWrite(4,HIGH); 57 | } 58 | else if(*data=='L') 59 | { 60 | digitalWrite(0,LOW); 61 | digitalWrite(2,HIGH); 62 | digitalWrite(3,HIGH); 63 | digitalWrite(4,LOW); 64 | } 65 | else if(*data=='R') 66 | { 67 | digitalWrite(0,HIGH); 68 | digitalWrite(2,LOW); 69 | digitalWrite(3,LOW); 70 | digitalWrite(4,HIGH); 71 | } 72 | else if(*data=='S') 73 | { 74 | digitalWrite(0,LOW); 75 | digitalWrite(2,LOW); 76 | digitalWrite(3,LOW); 77 | digitalWrite(4,LOW); 78 | } 79 | } 80 | } 81 | // close connection 82 | close(clientsocket); 83 | close(s); 84 | return 0; 85 | } 86 | -------------------------------------------------------------------------------- /Chapter09/VoiceControlBot.aia: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Hands-On-Robotics-Programming-with-Cpp/ae868642a390c956a3bf2a4ed0817cfad0013f19/Chapter09/VoiceControlBot.aia -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Packt 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | # Hands-On Robotics Programming with Cpp 5 | 6 | Hands-On Robotics Programming with Cpp 7 | 8 | This is the code repository for [Hands-On Robotics Programming with Cpp](https://prod.packtpub.com/hardware-and-creative/hands-robotics-programming-c?utm_source=github&utm_medium=repository&utm_campaign=9781789139006), published by Packt. 9 | 10 | **Leverage Raspberry Pi 3 and C++ libraries to build intelligent robotics applications** 11 | 12 | ## What is this book about? 13 | C++ is one of the most popular legacy programming languages for robotics, and a combination of C++ and robotics hardware is used in many leading industries. This book will bridge the gap between Raspberry Pi and C/C++ programming and enable you to develop applications for Raspberry Pi. To follow along with the projects covered in the book, you can implement C programs in Raspberry Pi with the wiringPi library. 14 | 15 | With this book, you’ll develop a fully functional car robot and write programs to move it in different directions. You’ll then create an obstacle - avoiding robot using an ultrasonic sensor. Furthermore, you’ll find out how to control the robot wirelessly using your PC/Mac. This book will also help you work with object detection and tracking using OpenCV, and guide you through exploring face detection techniques. Finally, you will create an Android app and control the robot wirelessly with an Android smartphone. 16 | 17 | By the end of this book, you will have gained experience in developing a robot using Raspberry Pi and C/C++ programming. 18 | 19 | This book covers the following exciting features: 20 | 21 | * Install software in Raspberry Pi compatible with C++ programming 22 | * Program the Raspberry Pi in C++ to run a motor 23 | * Control RPi-powered robot wirelessly with your laptop or PC 24 | * Program an RPi camera using OpenCV Control a Raspberry Pi robot with voice commands 25 | * Implement face and object detection with Raspberry Pi 26 | 27 | If you feel this book is for you, get your [copy](https://www.amazon.com/dp/1789139007) today! 28 | 29 | https://www.packtpub.com/ 31 | 32 | ## Instructions and Navigations 33 | All of the code is organized into folders. For example, Chapter02. 34 | 35 | **Following is what you need for this book:** 36 | This book is for developers, programmers, and robotics enthusiasts interested in leveraging C++ to build exciting robotics applications. Prior knowledge of C++ is necessary to understand the projects covered in this book. 37 | 38 | With the following software and hardware list you can run all code files present in the book: 39 | ### Software and Hardware List 40 | | Chapter | Software/Hardware required | OS required | 41 | | -------- | ------------------------------------ | ----------------------------------- | 42 | | All | Raspberry Pi Zero or Raspberry Pi B+ | Raspbian Stretch | 43 | 44 | 45 | We also provide a PDF file that has color images of the screenshots/diagrams used in this book. [Click here to download it](https://www.packtpub.com/sites/default/files/downloads/9781789139006_ColorImages.pdf). 46 | 47 | ### Related products 48 | * Learn Robotics Programming [[PACKT]](https://prod.packtpub.com/hardware-and-creative/learn-robotics-programming?utm_source=github&utm_medium=repository&utm_campaign=9781789340747) [[Amazon]](https://www.amazon.com/dp/1789340748) 49 | 50 | * Mastering ROS for Robotics Programming [[PACKT]](https://prod.packtpub.com/hardware-and-creative/mastering-ros-robotics-programming?utm_source=github&utm_medium=repository&utm_campaign=9781783551798) [[Amazon]](https://www.amazon.com/dp/1788478959) 51 | 52 | 53 | ## Get to Know the Author 54 | **Dinesh Tavasalkar** is a trainer and online instructor from India. He has trained more than 8,000+ students on topics related to robotics, Internet of Things, Arduino, Raspberry Pi, Android app development, augmented reality, and virtual reality. Dinesh's online courses have been undertaken by 25,000+ people on Udemy from more than 150+ countries. Some of his popular courses on Udemy include Robotics for beginners, Smartphone Control Robot using Arduino and Android, Build Augmented Reality apps using Unity and Vuforia, and Build Virtual Reality games for Google Cardboard using Unity. He also runs a YouTube channel called EngineersDream, where he teaches Android application development. 55 | 56 | 57 | **** 58 | 59 | 60 | ### Suggestions and Feedback 61 | [Click here](https://docs.google.com/forms/d/e/1FAIpQLSdy7dATC6QmEL81FIUuymZ0Wy9vH1jHkvpY57OiMeKGqib_Ow/viewform) if you have any feedback or suggestions. 62 | 63 | 64 | ### Download a free PDF 65 | 66 | If you have already purchased a print or Kindle version of this book, you can get a DRM-free PDF version at no cost.
Simply click on the link to claim your free PDF.
67 |

https://packt.link/free-ebook/9781789139006

--------------------------------------------------------------------------------