Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

ARDUINO C++ HELP! WILL RATE! Need some help finishing an encoder header file for

ID: 3888881 • Letter: A

Question

ARDUINO C++ HELP! WILL RATE!

Need some help finishing an encoder header file for an Arduino Uno bot :

Arduino bot: https://www.parallax.com/product/32335

Encoder: https://www.parallax.com/product/28107

Servo: http://www.mantech.co.za/Datasheets/Products/900-00008-65S.pdf

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 interrupts 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 (don't need to answer questions):
• 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?

Here's what I have so far with the encoder:

#ifndef __MY_ENCODERS__

#define __MY_ENCODERS__

//this function sets the tick counts to 0

void resetCounts();

unsigned long lEncCount = 0;

unsigned long rEncCount = 0;

//this function should return the left and right tickcounts

// since either the start of the program or the last

//call to the function resetCounts()

void getCounts(unsigned long counts[]);

bool resetCounts(){

//not sure here

}

unsigned long lEncLast = 0;

unsigned long rEncLast = 0;

int lEncVal = digitalRead(lEncPin);

int rEncVal = digitalRead(rEncPin);

  

if (lEncVal != lEncLast) lEncCount++;

if (rEncVal != rEncLast) rEncCount++;

lEncLast = lEncVal;

rEncLast = rEncVal;

//this function should return the instantaneous speeds of the wheels

//meassured in revolutions per second.

void getSpeeds(float speeds[]);

//this function should include whatever code necessary to initialize this module

void initEncoders();

int lEncPin = 10;

int rEncPin = 11;

//SET ENCODERS

pinMode(lEncPin, INPUT_PULLUP);

pinMode(rEncPin, INPUT_PULLUP);

#endif

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(RclickNum
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote