In this lesson we learn how to incorporate a push button switch into our Jetson Nano projects. We explain the concept of a pull up resistor, and show how to configure the GPIO pins as inputs. This will allow you to take your NVIDIA Jetson Nano projects to new heights. Enjoy!
Robotics Training LESSON 12: Programming Travel Distance with Infrared (IR) Remote
In this lesson we show how to use the Infrared (IR) Remote to program the Elegoo Smart Car. We use the remote to specify the distance in feet we want the robot car to travel. (If you guys want to play along at home, you can pick up your robot car HERE.)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 | #include <IRremote.h> int IRpin=12; IRrecv IR(IRpin); decode_results cmd; int ENA=5; int ENB=6; int IN1=7; int IN2=8; int IN3=9; int IN4=11; float d=1; int degRot=90; int left; int right; float v=1.2; void setup() { // put your setup code here, to run once: Serial.begin(9600); IR.enableIRIn(); IR.blink13(true); pinMode(ENA,OUTPUT); pinMode(ENB,OUTPUT); pinMode(IN1,OUTPUT); pinMode(IN2,OUTPUT); pinMode(IN3,OUTPUT); pinMode(IN4,OUTPUT); digitalWrite(ENA,HIGH); digitalWrite(ENB,HIGH); } void loop() { int wv; //v=1.2; //d=1; wv=(v-.35)/.0075; left=wv; right=wv; setSpeed(left,right); while (IR.decode(&cmd)==0){ } Serial.println(cmd.value,HEX); if (cmd.value==0xFF629D){ Serial.println("Forward"); forward(d,v); } if (cmd.value==0xFFA857){ Serial.println("Backward"); backward(d,v); } if (cmd.value==0xFFC23D){ Serial.println("Right"); turnRight(degRot,wv); } if (cmd.value==0xFF22DD){ Serial.println("Left"); turnLeft(degRot,wv); } //If Command Was Pound, Set Distance if (cmd.value==0xFF52AD){ Serial.println("Set distance"); delay(500); IR.resume(); while (IR.decode(&cmd)==0){ } delay(100); if (cmd.value==0xFF4AB5){ Serial.println("Distance=0"); d=0; } if (cmd.value==0xFF6897){ Serial.println("Distance=1"); d=1; } if (cmd.value==0xFF9867){ Serial.println("Distance=2"); d=2; } if (cmd.value==0xFFB04F){ Serial.println("Distance=3"); d=3; } if (cmd.value==0xFF30CF){ Serial.println("Distance=4"); d=4; } if (cmd.value==0xFF18E7){ Serial.println("Distance=5"); d=5; } if (cmd.value==0xFF7A85){ Serial.println("Distance=6"); d=6; } if (cmd.value==0xFF10EF){ Serial.println("Distance=7"); d=7; } if (cmd.value==0xFF38C7){ Serial.println("Distance=8"); d=8; } if (cmd.value==0xFF5AA5){ Serial.println("Distance=9"); d=9; } } //If Command Was star, Set Speed if (cmd.value==0xFF42BD){ Serial.println("Set speed"); delay(500); IR.resume(); while (IR.decode(&cmd)==0){ } delay(100); Serial.println(cmd.value,HEX); } IR.resume(); //calR(wv); //forward(8,v); //v=1.5; //wv=(v-.35)/.0075; //left=wv; //right=wv; //setSpeed(left,right); //backward(8,v); } void setSpeed(int leftVal,int rightVal){ analogWrite(ENA,leftVal); analogWrite(ENB,rightVal); } void forward(float d, float v){ float t; digitalWrite(IN1,HIGH); digitalWrite(IN2,LOW); digitalWrite(IN3,LOW); digitalWrite(IN4,HIGH); t=d/v*1000; delay(t); stopCar(); } void backward(float d, float v){ float t; digitalWrite(IN1,LOW); digitalWrite(IN2,HIGH); digitalWrite(IN3,HIGH); digitalWrite(IN4,LOW); t=d/v*1000; delay(t); stopCar(); } void turnRight(int deg, int wv){ float t; stopCar(); delay(100); analogWrite(ENA,125); analogWrite(ENB,125); digitalWrite(IN1,HIGH); digitalWrite(IN2,LOW); digitalWrite(IN3,HIGH); digitalWrite(IN4,LOW); t=(deg+6)/136.29*1000.; delay(t); stopCar(); analogWrite(ENA,wv); analogWrite(ENB,wv); } void turnLeft(int deg, int wv){ float t; analogWrite(ENA,125); analogWrite(ENB,125); digitalWrite(IN1,LOW); digitalWrite(IN2,HIGH); digitalWrite(IN3,LOW); digitalWrite(IN4,HIGH); t=(deg+6)/136.29*1000.; delay(t); stopCar(); analogWrite(ENA,wv); analogWrite(ENB,wv); } void stopCar(){ digitalWrite(IN1,LOW); digitalWrite(IN2,LOW); digitalWrite(IN3,LOW); digitalWrite(IN4,LOW); } void calF(){ digitalWrite(IN1,HIGH); digitalWrite(IN2,LOW); digitalWrite(IN3,LOW); digitalWrite(IN4,HIGH); delay(5000); stopCar(); } void calB(){ digitalWrite(IN1,LOW); digitalWrite(IN2,HIGH); digitalWrite(IN3,HIGH); digitalWrite(IN4,LOW); delay(5000); stopCar(); } void calR(int wv){ stopCar(); analogWrite(ENA,125); analogWrite(ENB,125); digitalWrite(IN1,HIGH); digitalWrite(IN2,LOW); digitalWrite(IN3,HIGH); digitalWrite(IN4,LOW); delay(3000); analogWrite(ENA,wv); analogWrite(ENB,wv); stopCar(); } void calL(int wv){ analogWrite(ENA,125); analogWrite(ENB,125); digitalWrite(IN1,LOW); digitalWrite(IN2,HIGH); digitalWrite(IN3,LOW); digitalWrite(IN4,HIGH); delay(5000); analogWrite(ENA,wv); analogWrite(ENB,wv); stopCar(); } |
Jetson Xavier NX Lesson 15: Training the Face Recognition Program to Recognize People
In this video lesson we should you a simple method to train our face recognizer on larger data sets. We use the python os.walk command to step through, and train automatically on all the training images in our folder. We then show how to store our training set to our SD card using the pickle utility. This allows us to train once, and use the trained model over and over.
For your convenience, the code below is what we developed to allow training our face recognition model.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | import face_recognition import cv2 import os import pickle print(cv2.__version__) Encodings=[] Names=[] j=0 image_dir='/home/pjm/Desktop/pyPro/demoimages/known' for root, dirs, files in os.walk(image_dir): print(files) for file in files: fullPath=os.path.join(root,file) print(fullPath) name=os.path.splitext(file)[0] print(name) person=face_recognition.load_image_file(fullPath) encoding=face_recognition.face_encodings(person)[0] Encodings.append(encoding) Names.append(name) print(Names) with open('train.pkl','wb') as f: pickle.dump(Names,f) pickle.dump(Encodings,f) |
Then this is a simple program that loads the trained model, and uses it to recognize people in unknown images.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | import cv2 print(cv2.__version__) import face_recognition import pickle with open('train.pkl','rb') as f: Names=pickle.load(f) Encodings=pickle.load(f) font=cv2.FONT_HERSHEY_SIMPLEX testImage=face_recognition.load_image_file('/home/pjm/Desktop/pyPro/demoimages/unkno$ facePositions=face_recognition.face_locations(testImage) allEncodings=face_recognition.face_encodings(testImage,facePositions) testImage=cv2.cvtColor(testImage,cv2.COLOR_RGB2BGR) for (top,right,bottom,left), face_encoding in zip(facePositions, allEncodings): name='Unknown Life Form' matches=face_recognition.compare_faces(Encodings,face_encoding) if True in matches: first_match_index=matches.index(True) name=Names[first_match_index] cv2.rectangle(testImage,(left,top),(right,bottom),(255,0,0),2) cv2.rectangle(testImage, (left,top),(left+200, top+30),(0,255,255),-1) cv2.putText(testImage,name,(left,top+20),font,.75,(255,0,0),2) cv2.imshow('mywindow',testImage) cv2.moveWindow('mywindow',0,0) if cv2.waitKey(0)==ord('q'): cv2.destroyAllWindows() |
AI on the Jetson Nano LESSON 56: Using the GPIO Pins on the Jetson Nano
In this lesson we show how to interact with the GPIO pins on the NVIDIA Jetson Nano. The GPIO pins on the Jetson Nano have very limited current capability, so you must learn to use a PN2222 BJT transistor in order to control things like LED or other components. In this lesson we show how the Jetson Nano can be used to control a standard LED.
Robotics Training LESSON 11: Controlling the Elegoo Smart Car With IR Remote
In this lesson we show how you can control a robot with an IR remote. We program the Elegoo Smart Car version 3.0 to respond to commands to the remote to go forward, backward, left and right.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 | #include <IRremote.h> int IRpin=12; IRrecv IR(IRpin); decode_results cmd; int ENA=5; int ENB=6; int IN1=7; int IN2=8; int IN3=9; int IN4=11; float d; int degRot=90; int left; int right; float v; void setup() { // put your setup code here, to run once: Serial.begin(9600); IR.enableIRIn(); IR.blink13(true); pinMode(ENA,OUTPUT); pinMode(ENB,OUTPUT); pinMode(IN1,OUTPUT); pinMode(IN2,OUTPUT); pinMode(IN3,OUTPUT); pinMode(IN4,OUTPUT); digitalWrite(ENA,HIGH); digitalWrite(ENB,HIGH); } void loop() { int wv; v=1.2; d=1; wv=(v-.35)/.0075; left=wv; right=wv; setSpeed(left,right); while (IR.decode(&cmd)==0){ } Serial.println(cmd.value,HEX); if (cmd.value==0xFF629D){ Serial.println("Forward"); forward(d,v); } if (cmd.value==0xFFA857){ Serial.println("Backward"); backward(d,v); } if (cmd.value==0xFFC23D){ Serial.println("Right"); turnRight(degRot,wv); } if (cmd.value==0xFF22DD){ Serial.println("Left"); turnLeft(degRot,wv); } IR.resume(); //calR(wv); //forward(8,v); //v=1.5; //wv=(v-.35)/.0075; //left=wv; //right=wv; //setSpeed(left,right); //backward(8,v); } void setSpeed(int leftVal,int rightVal){ analogWrite(ENA,leftVal); analogWrite(ENB,rightVal); } void forward(float d, float v){ float t; digitalWrite(IN1,HIGH); digitalWrite(IN2,LOW); digitalWrite(IN3,LOW); digitalWrite(IN4,HIGH); t=d/v*1000; delay(t); stopCar(); } void backward(float d, float v){ float t; digitalWrite(IN1,LOW); digitalWrite(IN2,HIGH); digitalWrite(IN3,HIGH); digitalWrite(IN4,LOW); t=d/v*1000; delay(t); stopCar(); } void turnRight(int deg, int wv){ float t; stopCar(); delay(100); analogWrite(ENA,125); analogWrite(ENB,125); digitalWrite(IN1,HIGH); digitalWrite(IN2,LOW); digitalWrite(IN3,HIGH); digitalWrite(IN4,LOW); t=(deg+6)/136.29*1000.; delay(t); stopCar(); analogWrite(ENA,wv); analogWrite(ENB,wv); } void turnLeft(int deg, int wv){ float t; analogWrite(ENA,125); analogWrite(ENB,125); digitalWrite(IN1,LOW); digitalWrite(IN2,HIGH); digitalWrite(IN3,LOW); digitalWrite(IN4,HIGH); t=(deg+6)/136.29*1000.; delay(t); stopCar(); analogWrite(ENA,wv); analogWrite(ENB,wv); } void stopCar(){ digitalWrite(IN1,LOW); digitalWrite(IN2,LOW); digitalWrite(IN3,LOW); digitalWrite(IN4,LOW); } void calF(){ digitalWrite(IN1,HIGH); digitalWrite(IN2,LOW); digitalWrite(IN3,LOW); digitalWrite(IN4,HIGH); delay(5000); stopCar(); } void calB(){ digitalWrite(IN1,LOW); digitalWrite(IN2,HIGH); digitalWrite(IN3,HIGH); digitalWrite(IN4,LOW); delay(5000); stopCar(); } void calR(int wv){ stopCar(); analogWrite(ENA,125); analogWrite(ENB,125); digitalWrite(IN1,HIGH); digitalWrite(IN2,LOW); digitalWrite(IN3,HIGH); digitalWrite(IN4,LOW); delay(3000); analogWrite(ENA,wv); analogWrite(ENB,wv); stopCar(); } void calL(int wv){ analogWrite(ENA,125); analogWrite(ENB,125); digitalWrite(IN1,LOW); digitalWrite(IN2,HIGH); digitalWrite(IN3,LOW); digitalWrite(IN4,HIGH); delay(5000); analogWrite(ENA,wv); analogWrite(ENB,wv); stopCar(); } |