In this lesson we connect the circuit and do the mechanical build for our self leveling platform. You can see a list of the gear we are using in LESSON 22.
Arduino Tutorial 44: Understanding Logical Shift Left and Logical Shift Right with the74HC595
In this lesson, we explore how to perform Logical Shift Left (LSL), and Logical Shift Right (LSR) functions on binary numbers, and we implement a circuit to perform these functions using an Arduino and a 74HC595 chip. We will demonstrate these functions on 8 bit binary numbers.
We start with the basic circuit and code which were developed in Lesson 42. In this lesson we are using parts from the Elegoo Arduino kit, which you can get HERE. We start with this circuit, which was explained in Lesson 42.
You can see that with this circuit, an 8 bit binary number can be visually displayed by illuminating the circuit LED. The goal of this lesson is to write code to perform LSL and LSR functions. The graphics below show conceptually how simple these functions are:
MSB stands for “Most Significant Bit” and LSB stands for “Least Significant Bit”. You can see that the LSL function just moves each bit one to the left, and fills the empty LSB with a “0”.
The LSR funtion is just as simple as illustrated below.
Such shifts are often required when doing digital logic, so it is important to understand what the terms mean.
We can see that the LSL function can be performed by simply multiplying the binary number by 2. Similarly the LSR function can be performed by dividing the binary number by 2.
Code for LSL:
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 | int latchPin=11; int clockPin=9; int dataPin=12; int dt=1000; byte myByte=0b10000000; //Put your binary number here void setup() { // put your setup code here, to run once: Serial.begin(9600); pinMode(latchPin,OUTPUT); pinMode(dataPin,OUTPUT); pinMode(clockPin,OUTPUT); } void loop() { // put your main code here, to run repeatedly: digitalWrite(latchPin,LOW); shiftOut(dataPin,clockPin,LSBFIRST,myByte); digitalWrite(latchPin,HIGH); Serial.println(myByte,BIN); delay(dt); myByte=myByte*2; } |
Code for LSR function:
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 | int latchPin=11; int clockPin=9; int dataPin=12; int dt=1000; byte myByte=0b10000000; void setup() { // put your setup code here, to run once: Serial.begin(9600); pinMode(latchPin,OUTPUT); pinMode(dataPin,OUTPUT); pinMode(clockPin,OUTPUT); } void loop() { // put your main code here, to run repeatedly: digitalWrite(latchPin,LOW); shiftOut(dataPin,clockPin,LSBFIRST,myByte); digitalWrite(latchPin,HIGH); Serial.println(myByte,BIN); delay(dt); myByte=myByte/2; } |
9-Axis IMU LESSON 22: How to Create a Tilt Stabilized Platform with Arduino
In this Lesson we begin to work on developing a tilt stabilized platform using the BNO055 9-axis sensor, and we will take advantage of all the learning that happened in the first 22 lessons. Now though, we will be moving out of the virtual world of Vpython, and will begin working in the real world. In this lesson we focus on getting the gear together. You can go ahead and order your gear, and then next week we will begin assembling and coding. In addition to the arduino nano, and the BNO055, you will need:
You Will Need Two of These HiTEC Servos
NOTE: I am no longer recommending the MG995 four pack of servos, as I have recently gotten several bad batches, so have moved to the HiTEC linked above.
A set of Pan Tilt Brackets:
PC Board Power Supply (If you have the ELEGOO Kit, you already have this)
Good Wall Wart Power Plug for the Power Supply (if you dont have one)
OK, get your gear ordered and we will start putting things together next week.
Arduino Tutorial 43: Binary Counter with 74HC595 Serial to Parallel Shift Register
In lesson 42 we showed you how to connect and program the 74HC595 shift register. We showed how data in byte format would then be written to an array of 8 LED to give a visual representation of the binary version of that byte variable. We then gave you the assignment to create a Binary Counter using the 4HC595. In this lesson we show you the solution. This builds on Lesson 42, so make sure to have your basic 74HC595 circuit set up before starting this lesson.
In this lesson we are using parts from the Elegoo Arduino kit, which you can get HERE.
The code we ended up developing in this lesson is provided below.
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 | int latchPin=11; int clockPin=9; int dataPin=12; int dt=1000; byte LED1s=0b00000000; void setup() { // put your setup code here, to run once: Serial.begin(9600); pinMode(latchPin,OUTPUT); pinMode(dataPin,OUTPUT); pinMode(clockPin,OUTPUT); } void loop() { // put your main code here, to run repeatedly: digitalWrite(latchPin,LOW); shiftOut(dataPin,clockPin,LSBFIRST,LED1s); digitalWrite(latchPin,HIGH); Serial.println(LED1s,BIN); delay(dt); LED1s=LED1s+1; } |
9-Axis IMU LESSON 21: Visualizing 3D Rotations in Vpython using Quaternions
In this lesson we show how to use quaternions from the BNO055 to create a visualization in Vpython. The visualization is a complete 3D free body rotation of a rigid body. To build this project you will need an Arduino Nano, and an Adafruit BNO055 Inertial Measurement Sensor.
This is the code we developed in the video posted here for your convenience. This code is for demo purposes only and should not be used in real applications. It is for educational purposes only.
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 | #include <Wire.h> #include <Adafruit_Sensor.h> #include <Adafruit_BNO055.h> #include <utility/imumaths.h> #include <math.h> #define BNO055_SAMPLERATE_DELAY_MS (100) Adafruit_BNO055 myIMU = Adafruit_BNO055(); void setup() { // put your setup code here, to run once: Serial.begin(115200); myIMU.begin(); delay(1000); int8_t temp=myIMU.getTemp(); myIMU.setExtCrystalUse(true); } void loop() { // put your main code here, to run repeatedly: uint8_t system, gyro, accel, mg = 0; myIMU.getCalibration(&system, &gyro, &accel, &mg); imu::Quaternion quat=myIMU.getQuat(); Serial.print(quat.w()); Serial.print(","); Serial.print(quat.x()); Serial.print(","); Serial.print(quat.y()); Serial.print(","); Serial.print(quat.z()); Serial.print(","); Serial.print(accel); Serial.print(","); Serial.print(gyro); Serial.print(","); Serial.print(mg); Serial.print(","); Serial.println(system); delay(BNO055_SAMPLERATE_DELAY_MS); } |
This is the code we developed on the python side to do the visualization from the passed quaternions.
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 | from vpython import * from time import * import numpy as np import math import serial ad=serial.Serial('com5',115200) sleep(1) scene.range=5 scene.background=color.yellow toRad=2*np.pi/360 toDeg=1/toRad scene.forward=vector(-1,-1,-1) scene.width=1200 scene.height=1080 xarrow=arrow(lenght=2, shaftwidth=.1, color=color.red,axis=vector(1,0,0)) yarrow=arrow(lenght=2, shaftwidth=.1, color=color.green,axis=vector(0,1,0)) zarrow=arrow(lenght=4, shaftwidth=.1, color=color.blue,axis=vector(0,0,1)) frontArrow=arrow(length=4,shaftwidth=.1,color=color.purple,axis=vector(1,0,0)) upArrow=arrow(length=1,shaftwidth=.1,color=color.magenta,axis=vector(0,1,0)) sideArrow=arrow(length=2,shaftwidth=.1,color=color.orange,axis=vector(0,0,1)) bBoard=box(length=6,width=2,height=.2,opacity=.8,pos=vector(0,0,0,)) bn=box(length=1,width=.75,height=.1, pos=vector(-.5,.1+.05,0),color=color.blue) nano=box(lenght=1.75,width=.6,height=.1,pos=vector(-2,.1+.05,0),color=color.green) myObj=compound([bBoard,bn,nano]) while (True): try: while (ad.inWaiting()==0): pass dataPacket=ad.readline() dataPacket=str(dataPacket,'utf-8') splitPacket=dataPacket.split(",") q0=float(splitPacket[0]) q1=float(splitPacket[1]) q2=float(splitPacket[2]) q3=float(splitPacket[3]) roll=-math.atan2(2*(q0*q1+q2*q3),1-2*(q1*q1+q2*q2)) pitch=math.asin(2*(q0*q2-q3*q1)) yaw=-math.atan2(2*(q0*q3+q1*q2),1-2*(q2*q2+q3*q3))-np.pi/2 rate(50) k=vector(cos(yaw)*cos(pitch), sin(pitch),sin(yaw)*cos(pitch)) y=vector(0,1,0) s=cross(k,y) v=cross(s,k) vrot=v*cos(roll)+cross(k,v)*sin(roll) frontArrow.axis=k sideArrow.axis=cross(k,vrot) upArrow.axis=vrot myObj.axis=k myObj.up=vrot sideArrow.length=2 frontArrow.length=4 upArrow.length=1 except: pass |