In this section you will implement a library to use the wheel encoders. Encoders
ID: 3886976 • Letter: I
Question
In this section you will implement a library to use the wheel encoders. Encoders allow the robot
to get feedback on motor rotations by counting the number of holes encountered by the encoder.
Encoders are needed since a servo’s response to the control signal varies due to several factors
including floor friction, load on the motors, noise, etc. For this section, you must use interrupts3
to implement the 4 functions on the header file “MyEncoders.h”:
1.void resetCounts()
2.void getCounts(unsigned long counts[])
3.void getSpeeds(float speeds[])
4.void initEncoders()
The first function should reset the tick count (number of holes counted) to zero.
The second function should return the left and right tick counts since the last call to resetCounts, or since the start of the program (if there were no calls to resetCounts).
The third function should return the instantaneous left and right wheel speeds (measured in revolutions per second).
The fourth function should contain whatever code is necessary for initialization.
You are allowed to modify the header file but all those functions need to be implemented.
Aspects to consider:
Each encoder has 32 equidistant holes; thus, they are separated by 1/32 rotations.
Sometimes, if the wheels shake, the values read by the encoders might oscillate making the tick count increase very fast. You should take this into account.
The maximum speed of the servos is approximately 0.80 revolutions per second, so the shortest time interval between two holes is approximately 39ms. At half speed, the interval increases to 78ms, at one fourth it increases to 156ms, and so on. These intervals are very large when compared to the speed at which a processor can run. For this reason, it is possible to measure the speed when you haven’t even counted 1 tick. This is especially true at slow speeds. If you haven’t seen any new ticks since the last time you called getSpeeds, and you try to get a new measure what speed should you return? How do you know whether the robot is moving very slow or just standing still?
Do the encoders used in the course provide information on the direction that the wheel has moved (forward / backward)?
MyEncoders.h
Explanation / Answer
#include //the motor driver uses this library #include Servo LEFT, RIGHT;//left wheel right wheel int RclickNum=0;//used for the rotory encoder int LclickNum=0;//these are the number of "clicks" each wheel has moved int D =115;//Drive int R =70;//Reverse int B =90;//Break int Linterrupt = 1;//these are the interrupt numbers. 0 = pin 3 and 1 = pin 2 int Rinterrupt = 0; int clickConvert = 7;// how many rotary encoder clicks equal a foot void setup() { Serial.begin(9600); //starting serial communication LEFT.attach( 9, 1000, 2000);//attaching the motor controller that is acting like a servo RIGHT.attach(10, 1000, 2000); attachInterrupt(Linterrupt, LclickCounter, FALLING);//attaching the rotary encoders as interrupts that will attachInterrupt(Rinterrupt, RclickCounter, FALLING);//trip when the encoder pins go from high to low } void loop() {//This is for controling the robot using the standard wasd format int input= Serial.read(); if(input == 'a') left(2); if(input == 'd') right(2); if(input == 'w') forward(2); if(input == 's') backward(2); if(input == 'e') STOP(); } void forward(int feet)//this is called when w is sent threw the serial port and is where i am testing all of my code. { interrupts(); //turn on the interrupts while(RclickNumRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.