Untitled

mail@pastecode.io avatar
unknown
plain_text
15 days ago
2.1 kB
3
Indexable
Never
// lab #3 Q2

#include <Servo.h> // include file for servo control  //UNDERSTOOD

Servo servo1;  // create servo object to control servo #1   //UNDERSTOOD
 
float t0 = 0.0; // need to make t0 a global variable so loop and setup can share it  //UNDERSTOOD

void setup() {

	servo1.attach(7);  // connect pin 7 to servo #1  //UNDERSTOOD

	// initialize serial communication at 115200 bits per second
	Serial.begin(115200);  //ASKED
	
	// measure initial time in s 
	// make sure to measure t0 last in setup since the other parts //Q1: since the t0 measure the time when the setup start should we not put it at the begining
	// of setup will take time to perform. //UNDERSTOOD
	t0 = micros()*1.0e-6; //UNDERSTOOD

}

void loop() {
	// note this program could also be put in setup() after Serial.begin  //UNDERSTOOD
	
	// note this loop function gets executed repeatedly unlike Q1 // C1:is it because the exit() has a 1 in it and in the Q1 it has a 0 in it
	// -- see if you can spot the difference
	
	float t; // clock time in s  //UNDERSTOOD
	float A, w, phi1;  //UNDERSTOOD
	int theta1_d; // desired angle for servo #1   //UNDERSTOOD

	A = 45.0;  //UNDERSTOOD
	w = 1.0; // higher values of w will make the servos oscillate faster  //UNDERSTOOD
	phi1 = 0.0;  //UNDERSTOOD
	
	// calculate time t since the program begins  
	// note: since micros() gives the time since the program (i.e.) setup begins
	// calculating t = micros()*1.0e-6 will be close to t = micros()*1.0e-6 - t0   //UNDERSTOOD
	// but not as accurate since t0 accounts for the time taken to execute setup()	  //UNDERSTOOD
	t = micros()*1.0e-6 - t0;  //UNDERSTOOD
		
	theta1_d = A*( 1.0 + sin(w*t + phi1) );  //UNDERSTOOD

	servo1.write(theta1_d); // command servo #1 to go to theta1_d  //UNDERSTOOD

	// note we don't have to wait/delay for this since the desired angles  //C2: we don't have to delay because the equation itself control the time ?
	// are changing continuously and slowly as functions of time

	if(t > 60.0) exit(1); // end program at t=60s  //ASKED

}
Leave a Comment