Post 6, week something

First of all, for my update on learning, check out my personal blogging site that has just been completely rebuilt (it may look empty).

Update: no need to check there anymore, I copied and pasted the content below.


Over the past few weeks, I have been working to figure out the logic and structure of my car’s code. After all, I have to do some planning. This involves a lot of counting and it is like probability, where you have to consider every possibility. I have 4 sensors that I am working with, and I have to figure out how their input will effect my motors in different situations, like when the car is just put on the track. Now, I have basically finished all the logic, but have yet to actually write the code.

These are some of my logic diagrams. I tried doing a mind map, but I think it is better if I do this first and then maybe see if I need a mind map

Another thing that I have to figure out is how I can write my code. Obviously, I can do like fifty “if…” statements for every possible input, but that is very tiresome, and it does not look professional. My dad suggested a statement, but he doesn’t know python, and that statement is only for C. Other statements that people suggested to me, I don’t understand at all, so right now I am reading several books on python and doing an online course hosted by U of Waterloo.

It’s called Python from Scratch

I haven’t started writing the code yet, but I know I should really hurry up, because if my code don’t work, oh well, I won’t have anything to present on in-depth night! So I should probably allow some time for me to debug and add for functions that I would love to have, if I have time. These are things like a new mode that uses 3 ultrasonic sensors, online viewing and control from the car’s Raspi camera, etc.

image3 image1 image4

 

Below is some example code I have:


# -*- coding: utf-8 -*-
import time 
import RPi.GPIO as GPIO
import datetime as dt


GPIO.setwarnings(False) #so no error messeges

 
GPIO.setmode(GPIO.BCM)#setting the mode for the GPIO pin numberings so the computer grabs the right pin!
GPIO.setup(24, GPIO.OUT)#Lmotor main 
GPIO.setup(25, GPIO.OUT)#Lmotor
GPIO.setup(26, GPIO.OUT)#Rmotor main
GPIO.setup(27, GPIO.OUT)#Rmotor
UsonicCenterTRIGR = 22#ultrasonic
UsonicCenterECHO = 23#ultrasonic
GPIO.setup(UsonicCenterTRIGR,GPIO.OUT)#ultrasonic
GPIO.setup(UsonicCenterECHO,GPIO.IN)#ultrasonic
UsonicLeftTRIGR = 12#ultrasonic
UsonicLeftECHO = 13#ultrasonic
GPIO.setup(UsonicLeftTRIGR,GPIO.OUT)#ultrasonic
GPIO.setup(UsonicLeftECHO,GPIO.IN)#ultrasonic
UsonicRightTRIGR = 16#ultrasonic
UsonicRightECHO = 17#ultrasonic
GPIO.setup(UsonicRightTRIGR,GPIO.OUT)#ultrasonic
GPIO.setup(UsonicRightECHO,GPIO.IN)#ultrasonic
linesensorCenter=5#line sensor C
GPIO.setup(linesensorCenter,GPIO.IN)#line sensor C
linesensorLeft=4#line sensor L
GPIO.setup(linesensorLeft,GPIO.IN)#line sensor L
linesensorRight=6#line sensor R
GPIO.setup(linesensorRight,GPIO.IN)#line sensor R

pwm = GPIO.PWM(24, 50) #50hz
pwm1 = GPIO.PWM(25, 50)
pwm2 = GPIO.PWM(26, 50) #50hz
pwm3 = GPIO.PWM(27, 50)
pwm.start(0) #Load:100-full right now we have no pwm
pwm1.start(0) #Load:100-full right now we have no pwm
pwm2.start(0) #Load:100-full right now we have no pwm
pwm3.start(0) #Load:100-full right now we have no pwm

 #defining the duty cycle, which is the % the motor is working, controls speed

time.sleep(1)
print ("initialized")

#functions help make code neater and organized, and i can just call on these functions later to summon them
def ending(): #the stopping sequence
 print "ending"
 pwm.stop()
 pwm1.stop()
 pwm2.stop()
 pwm3.stop()


 GPIO.output(24, False) #if both motor on same side have 1 or 0 then stop
 GPIO.output(25, False)
 GPIO.output(26, False)
 GPIO.output(27, False)
 GPIO.cleanup()

def stop(): #the stopping sequence
 print "stopping"
 pwm.stop()
 pwm1.stop()
 pwm2.stop()
 pwm3.stop()


 GPIO.output(24, False) #if both motor on same side have 1 or 0 then stop
 GPIO.output(25, False)
 GPIO.output(26, False)
 GPIO.output(27, False)

def goforward(dc,t): 
 GPIO.output(24, True) #if the main is 1, then forward, if the other one is 1, then backward
 GPIO.output(25, False)
 GPIO.output(26, True)
 GPIO.output(27, False)
 pwm.ChangeDutyCycle(dc)
 pwm2.ChangeDutyCycle(dc)
 time.sleep(t)

def gobackward(dc,t):
 GPIO.output(24, False)
 GPIO.output(25, True)
 GPIO.output(26, False)
 GPIO.output(27, True)

 pwm1.ChangeDutyCycle(dc)
 pwm3.ChangeDutyCycle(dc)
 time.sleep(t)

def gofwdleft(dc,t):
 GPIO.output(24, False)
 GPIO.output(25, False)
 GPIO.output(26, True)
 GPIO.output(27, False)

 pwm2.ChangeDutyCycle(dc)
 time.sleep(t)

def gofwdright(dc,t):
 GPIO.output(24, True)
 GPIO.output(25, False)
 GPIO.output(26, False)
 GPIO.output(27, False)

 pwm.ChangeDutyCycle(dc)
 time.sleep(t)

def gobwdleft(dc,t):
 GPIO.output(24, False)
 GPIO.output(25, False)
 GPIO.output(26, False)
 GPIO.output(27, True)

 pwm3.ChangeDutyCycle(dc)
 time.sleep(t)

def gobwdright(dc,t):
 GPIO.output(24, False)
 GPIO.output(25, True)
 GPIO.output(26, False)
 GPIO.output(27, False)

 pwm1.ChangeDutyCycle(dc)
 time.sleep(t)

def gohardright(dc,t):
 GPIO.output(24, False)
 GPIO.output(25, True)
 GPIO.output(26, True)
 GPIO.output(27, False)

 pwm1.ChangeDutyCycle(dc)
 time.sleep(t)
 
def gohardleft(dc,t):
 GPIO.output(24, True)
 GPIO.output(25, False)
 GPIO.output(26, False)
 GPIO.output(27, True)

 pwm1.ChangeDutyCycle(dc)
 time.sleep(t)

##def testingmode():
## print ("going forwards")
## goforward(90,1)
## goforward(75,1)
## stop()
## time.sleep(1)
## print ("going backwards")
## gobackward(90,1)
## gobackward(75,1)
## stop()
## time.sleep(1)
## print ("fwd right and left")
## gofwdright(90,1)
## gofwdleft(90,1)
## stop()
## time.sleep(1)
## print ("backward right and left")
## gobwdright(90,1)
## gobwdleft(90,1)
## stop()
## time.sleep(1)
## print ("hard right and left")
## gohardright(90,1)
## gohardleft(90,1)
## ending()
 
def start():
 print ("starting")
 while linesensorCenter == 0 and linesensorLeft == 0 and linesensorRight == 0:
 if linesensorCenter and linesensorLeft and linesensorRight == 0:
 goforward (90)
 elif linesensorCenter == 1:
 goforward (90)
 main()
 elif linesensorRight == 1:
 goforward(75)


##testingmode()


##speedchange=1 
##while speedchange:
## for dc in range(50,100, 5):
## pwm.ChangeDutyCycle(dc)
## pwm1.ChangeDutyCycle(dc)
## time.sleep(2)
## print dc,"duty cycle"
## for dc in range(100,50, -5):
## pwm.ChangeDutyCycle(dc)
## pwm1.ChangeDutyCycle(dc)
## time.sleep(2)
## print dc, "duty cycle"
## #minimum 50, 70 to run
##if KeyboardInterrupt:
## speedchange=0
## ending() 

De Bono time:

Here is a list of concepts that my mentor and I talked about last meeting:

  • digitalized learning
  • machine learning
  • logic of a computer
  • human errors
  • debug
  • education
  • alternatives
  • humanity
  • profit vs. everything else
  • etc.

Coincidently enough, during our conversation, which went really off-topic (shhh), we talked and discussed a lot

Courtesy of giphy.com

about the United Airline incident that happened recently. When I went back to listen to our conversation, I realized that it was the part during our talk in which we brought up the most alternatives! To start, my mentor suggested for us to look at this incident from another perspective.

The passenger that was violently dragged off was actually a terrorist under disguise. The FBI have been tracking him for days, but never found an opportunity to take him down without alerting the media or the public. So they told the flight attendant to basically pick this passenger when they are doing the draw. This way, they can get the job done, and the public or foreign agencies will never know of the secrets they can extract.

Another explanation for the event doesn’t necessarily praise United, but it claims its innocence:

 

These explanations for the event look at it from another perspective, and offer an alternative way of thinking about the event.

Anyways, back to my actual topic of alternatives, I had a lot of alternatives when considering my car design:

  • how will it work? (line following? obstacle avoiding? real time control? )

    giphy.com
  • how should I write and organize the code?
    • using a lot of if statements?
    • finding a way of listing input that will direct me to a certain output?
    • use “def” makes the code more readable than not using it, but it is more work
    • comment makes the code more readable by humans and make me remember what everything does, but is a lot of work as well
  • if I don’t have time, I could just do one mode (line following + one ultrasonic)
  • if the motors don’t work well going at 70% power, I can just not do slower speeds
  • maybe create two different tracks for two different modes, if I am going to have that