Day 2: (Aug 29) Temporal Coding

Notes for 2018-08-29. See also the Fall 2018 Calendar.

Notes from Day 1

We have a broad range of experience levels in the class; everybody needs to identify their own knowledge frontier and help shape the right scope.

Agenda

  1. Administrative
  2. Assignments
  3. In-class
    1. Introduce the assignments.
    2. Walk through the 16-223 WordPress.
    3. Introduction to hobby servos.
    4. Show Exercise: Servo Sweep.
    5. Walk through Sample Arduino Sketches.
    6. Arduino programming discussion.
    7. Periodic table activity: generate two questions.
    8. Time permitting: start work on technical demo.

Lecture code samples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
// demo the firmware clocks, debugging on the serial port
void setup()
{
  Serial.begin(115200);
}

long last_time = 0;

void loop()
{
  long now = micros();

  Serial.print(millis());
  Serial.print(" ");  

  Serial.print(micros());
  Serial.print(" ");

  Serial.println(now - last_time);

  last_time = now;
}
1
2
3
4
5
6
7
8
9
// demo IDE graphing: see Tools/Serial Plotter
void setup()
{
  Serial.begin(115200);
}
void loop()
{
  Serial.println(sin(4 * 6.28 * 0.001 * millis()));
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
// demo use of a library, hobby servo output
#include <Servo.h> 

Servo your_chosen_servo_name;

void setup()
{
  your_chosen_servo_name.attach(9);
}

void loop()
{
  your_chosen_servo_name.write(45);
  delay(500);

  your_chosen_servo_name.write(135);
  delay(500);
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
// demo use of a lookup table
#include <Servo.h> 

const int SERVO_PIN = 9;
Servo svo;

void setup(void)
{
  svo.attach(SERVO_PIN);
}

const int angles[12] = { 45, 0, 135, 0, 90, 0, 90, 45, 135, 90, 180, 0 };

void loop(void)
{
  for (int idx = 0; idx < 12; idx = idx + 1) {
    svo.write(angles[idx]);
    delay(500);
  }
}
 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
// demo use of a trajectory generator function
#include <Servo.h> 

const int SERVO_PIN = 9;
Servo wiggling_servo;

void setup(void)
{
  Serial.begin(115200);
  wiggling_servo.attach(SERVO_PIN);
}


void loop(void)
{
  // Perform a smooth movement around the center several times.
  for (int i = 0; i < 4; i = i+1) {

    // Define a few constants governing the motion.  Note that this example
    // uses a C++ style of declaration which looks more like a normal variable
    // declaration, but whose value cannot be changed.
    const float center    = 90.0;     // in degrees
    const float magnitude = 30.0;     // in degrees
    const float period    =  4.0;     // in seconds, duration of cycle
    const float interval  =  0.020;   // in seconds, duration of each step

    int cycle_steps = period / interval;
    
    for (int step = 0; step < cycle_steps; step++) {
      // Compute the 'phase angle' for the sine function.  Note that the sin()
      // function requires an angle in radians.
      float phase = step * (2*M_PI/cycle_steps);
      
      // Compute the angle to send to the servo.
      float angle = center + magnitude * sin(phase);
      wiggling_servo.write(angle);

      // Wait for one sampling period.
      delay(1000*interval);
    }
  }
  Serial.println("cycle done.");
}
 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
// demo use of a differential equation generator function
#include <Servo.h> 

const int SERVO_PIN = 9;
Servo svo;

// ================================================================
// Simple Harmonic Motion oscillator, e.g. unit-mass on spring with damping.

const float k    = 4*M_PI*M_PI;   // 1 Hz; freq = (1/2*pi) * sqrt(k/m); k = (freq*2*pi)^2
const float b    = 1.0;           // damping
const float q_d  = 90.0;          // neutral spring position
const float dt   = 0.01;          // integration time step

float q    = 0.0;         // initial position
float qd   = 0.0;         // initial velocity

// ================================================================

void setup(void)
{
  Serial.begin(115200);
  svo.attach(SERVO_PIN);
}

// ================================================================
void loop()
{
  // calculate the derivatives
  float qdd = k * (q_d - q) - b * qd;

  // integrate one time step
  q  += qd  * dt;
  qd += qdd * dt;

  // update the servo
  svo.write(q);

  // logic to reset the oscillator after a cycle has completed
  if (fabs(qd) < 0.1 && fabs(q_d - q) < 0.1) q = 0.0;
      
  // print the output for plotting
  Serial.println(q);

  // delay to control timing
  delay((int)(1000*dt));
}