Day 9: (Sep 26) Feedback

Notes for 2018-09-26. See also the Fall 2018 Calendar.

Notes from Day 8

Agenda

  1. Administrative
    • Who has not yet been fingerprinted?
    • Who has received an FBI letter?
  2. Assignments
  3. In-class
    1. Discussion of signal processing.
      1. Sampling, resolution, and noise.
      2. Low-pass filtering.
      3. Velocity estimation.
      4. Feedback and PID control.
    2. Individual demo 5 meetings

Lecture code samples

On Day 8: (Sep 24) Signals appeared the smoothing and ring filter samples.

Feedback

 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
// proportional feedback example using a dual H-bridge driver

#define MOT_A1_PIN 5
#define MOT_A2_PIN 6
#define MOT_B1_PIN 9
#define MOT_B2_PIN 10

void setup(void)
{
  pinMode(MOT_A1_PIN, OUTPUT);
  pinMode(MOT_A2_PIN, OUTPUT);
  pinMode(MOT_B1_PIN, OUTPUT);
  pinMode(MOT_B2_PIN, OUTPUT);

  digitalWrite(MOT_A1_PIN, LOW);
  digitalWrite(MOT_A2_PIN, LOW);
  digitalWrite(MOT_B1_PIN, LOW);
  digitalWrite(MOT_B2_PIN, LOW);

  Serial.begin(115200);
}

/****************************************************************/
void set_motor_pwm(int pwm, int IN1_PIN, int IN2_PIN)
{
  if (pwm < 0) {  // reverse speeds
    analogWrite(IN1_PIN, -pwm);
    digitalWrite(IN2_PIN, LOW);

  } else { // stop or forward
    digitalWrite(IN1_PIN, LOW);
    analogWrite(IN2_PIN, pwm);
  }
}

/****************************************************************/
void loop(void)
{
  static unsigned long last_update_clock = 0;

  unsigned long now = micros();
  unsigned long interval = now - last_update_clock;
  last_update_clock = now;

  const long sample_interval = 10000;   // 10 msec, 100 Hz sampling
  static long sample_timer = 0;
  static float position = 0.0;     // filtered value of the input (unit normalization)
  static float target = 0.5;       // target position (unit normalization)
  
  sample_timer -= interval;
  if (sample_timer <= 0) {
    sample_timer += sample_interval;

    int raw_value = analogRead(0);                       // read the current input 
    float calibrated = ((float)raw_value) / 1024.0;      // scale to normalized units 
  
    float difference  =  calibrated - position;   // compute the 'error'
    position         += 0.2 * difference;         // apply a constant gain to move the smoothed value toward the reading

    // calculate a control update
    float position_error = target - position;
    float control_output = 2.0 * position_error;  // apply a proportional position gain

    int control_pwm = constrain((int)(256.0 * control_output), -255, 255);
    set_motor_pwm(control_pwm, MOT_A1_PIN, MOT_A2_PIN);
  }
}