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

Write a Matlab program that calculates the peak-to-peak value of the servomotor

ID: 3348947 • Letter: W

Question

Write a Matlab program that calculates the peak-to-peak value of the servomotor shaft oscillation. The program must be fully documented with meaningful comments. In other words your program will take an array of 2000 elements and calculate the peak to peak value. (Hint: One way is to identify the maximum and minimum value of the encoder signal over a number of cycles, for example 20. Then take the average to identify the average peak-to-peak value. There are also other ways of doing this such as RMS detection)

Explanation / Answer

#include <stdio.h>
#include “c:Program Filessensoraydriverss626.h”
#include “c:Program Filessensoraydriverss626f.h”
#define CRA1 0x00
#define CRB1 0x02
#define PRE1ALSW 0x0C /* register offset values */
#define PRE1AMSW 0x0E
#define LATCH1ALSW 0x0C
#define LATCH1AMSW 0x0E
void main()
{
SYSTEM system;
ECODE ecode;
HBD hbd = 0;
WORD ldata, hdata; /* unsigned short (16bits) */
int enc_count; /* where encoder data is stored */
int start_count; /* remember beginning count */
printf(“encoder test ”);
S626_Init(&system);
/* Module1: counter initialization */
S626_RegWrite(hbd,PRE1ALSW,0x0000);
S626_RegWrite(hbd,PRE1AMSW,0x0080);
S626_RegWrite(hbd,CRA1,0x016f);
S626_RegWrite(hbd,CRA1,0x0360);
S626_RegWrite(hbd,CRB1,0x0C50);
/* Module2: read starting encoder position */
/* read in low 16 bits into variable ldata */
S626_RegRead(hbd, LATCH1ALSW, &ldata);
/* read in bits 23 to 16 into variable hdata */
S626_RegRead(hbd, LATCH1AMSW, &hdata);
/*left shift hdata by 16 bits */
enc_count = ((int) hdata) << 16;
start_count = enc_count | ((int) ldata); /* get starting
positon reading*/
while(!kbhit()) {
/*Module3: read the current position of motor shaft */
S626_RegRead(hbd, LATCH1ALSW, &ldata);
S626_RegRead(hbd, LATCH1AMSW, &hdata);
enc_count = ((int) hdata) << 16;
enc_count = enc_count | ((int) ldata);
/* subtract start position reading in order to start
from 0 since a relative encoder is used */
enc_count -= start_count;
putchar(‘ ’);
printf(“%10d”, enc_count);
}
S626_Close();
} /* end of main */

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