HTML('''<script>
code_show=true;
function code_toggle() {
if (code_show){
$('div.input').hide();
} else {
$('div.input').show();
}
code_show = !code_show
}
$( document ).ready(code_toggle);
</script>
<form action="javascript:code_toggle()"><input type="submit" value="Click here to toggle on/off the raw code."></form>''')
import numpy
import matplotlib.pyplot as plt
from IPython.display import Audio
from IPython.display import Image
from scipy import signal
from scipy.fft import fftshift
from scipy.io import wavfile
plt.rcParams['figure.figsize'] = [12, 3]
from IPython.core.display import HTML
HTML("""
<style>
.output_png {
display: table-cell;
text-align: center;
vertical-align: middle;
}
</style>
""")
In this lab, you will give a very simple robot a personality.
The nature of this lab is a bit different than that of the other labs. In all of the previous laboratory assignments, you built tools of some variety. In the first lab, you built a tool for understanding birdsong. In the second, you built a tool for understanding flocking behavior. In the third lab, you built a tool for doing realtime frequency analysis. The outcome of this lab will not be a tool that could be used for some other purpose. Instead, the objective of this lab is to build something that makes you smile. The extent to which a couple of simple behaviors can give a robot a personality is remarkable. In this lab, you are tasked with creating that personality, subject to a couple constraints. One of those constraints is that the robot must react to your voice.
The "robot" used for this lab is simply composed of two micro-servos (SG90) arranged in a pan/tilt configuration. A passive piece of hardware is mounted on top of the assembly to give the appearance of eyes. Every behavior that you program will be a sequence of pan/tilt commands to the servos.
Each of these servos is controlled using pulse-width modulation. The period of the PWM signal is 20 ms (50 Hz), and the length of the pulse is 1-2 ms. A pulse-width of 1 ms corresponds to a "left" position of the servo (which can rotate 180 degrees). A pulse-width of 1.5 ms corresponds to the center position, and 2 ms corresponds to the right position. See the image below.
Note: Because of the physical arrangement of the assembly, the "tilt" servo is constrained in the range of 90-180 degrees. It cannot look "down," in other words.
In order to generate these PWM signals, you will need to setup two output compare units on a 32-bit timer. See the timer page for information about setting up a 32-bit timer and output-compare units. Note that you will need to configure the output compare units to 32-bit mode, as shown below for OC3:
OpenOC3(OC_ON | OC_TIMER_MODE32 | OC_TIMER2_SRC | OC_PWM_FAULT_PIN_DISABLE , 0, 0) ;
PPSInput(2, U2RX, RPA1)
PPSOutput(4, RPB10, U2TX)
#define use_art_serial
in the config file.PPSOutput(4, RPA3, OC3) ; // configure OC3 to RPA3
PPSOutput(3, RPA2, OC4) ; // configure OC4 to RPA2
SetChanADC10( ADC_CH0_NEG_SAMPLEA_NVREF | ADC_CH0_POS_SAMPLEA_AN11 ); // configure to sample AN11
RPA3
RPA2
Here is a suggestion for how to organize your program.
- Sets the duty cycle for the PWM signals to each motor
- Gathers of sample from the ADC
- If that sample exceeds a certain threshold (i.e. volume), signals Personality Thread to return the motors to their pre-programmed positions.
- Opens 32-bit interrupt timer and configures interrupt
- Opens and configures ADC
- Opens and configures output compare modules
- Sets up protothreads and schedules tasks round-robin
- Waits for serial command from Python user interface
- Places system into "Programming" or "Active" mode based on state of Python toggle switch
- In "Programming" mode, sets position of servos with slider values and stores those positions in global variables
- In the absence of audio input, manipulates the PWM signals to each motor to exhibit some personality
- When audio input is received, returns the motors to their pre-programmed positions (either instantly or slowly). The motors should remain in that position for a programmed amount of time, and then return to their personality-exhibiting mode.
By the end of lab section in week one you must have:
Write a Protothreads C program which will:
Write a Python program which will:
At no time during the demo can you reset or reprogram the MCU.
Your written lab report should include the sections mentioned in the policy page, and: