Skip to content

Commit

Permalink
Add feature that allow user change the motor speed when running
Browse files Browse the repository at this point in the history
  • Loading branch information
WangHanChi committed Jan 22, 2024
1 parent fa2534b commit 7975136
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 9 deletions.
39 changes: 31 additions & 8 deletions script/plot.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,40 @@
import matplotlib.pyplot as plt
import numpy as np

data = np.genfromtxt('data.txt', delimiter=' ->', dtype=float)
# Initialize lists to store time and RPM values
time_values = []
rpm_values = []

##########
TicksPerSecond = 20 # <--- ONLY MODIFY HERE
##########
# Set the TicksPerSecond variable
TicksPerSecond = 20 # <--- ONLY MODIFY HERE

time_interval = (1 / TicksPerSecond)
time = np.arange(0, len(data) * time_interval, time_interval)
speed = data[:, 1]
# Read data from file
with open('data.txt', 'r') as file:
for line in file:

plt.plot(time, speed)
# check if the line is blank or not
if not line.strip():
continue

try:
# Split the line using '|' as a delimiter
data = line.strip().split('|')
# Check if there are at least three '|' symbols
if len(data) < 4:
continue

# Extract time and RPM values from the data
time = float(data[1].strip())
rpm = float(data[3].strip())

# Append values to the lists
time_values.append(time / TicksPerSecond)
rpm_values.append(rpm)

except ValueError as e:
continue # Skip lines with incorrect format

plt.plot(time_values, rpm_values)
plt.xlabel('Time (s)')
plt.ylabel('Speed (rad/s)')
plt.title('Speed Response')
Expand Down
15 changes: 14 additions & 1 deletion src/user/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ static void vspeedTask(void *params)
xTaskNotifyWait(0, 0, NULL, portMAX_DELAY);
speed_A = 60 * (pulse_A - pre_pulse_A) * (20.0) / 1000.0 / (1.785);
if (display_speed) {
xsprintf(message, "%d ->%d \n\r", count++, (int) speed_A);
xsprintf(message, "|%d| -> |%d| RPM\n\r", count++, (int) speed_A);
ptransmit = message;
xQueueSend(uart_write_queue, &ptransmit, portMAX_DELAY);
}
Expand Down Expand Up @@ -322,6 +322,8 @@ void command_pmdc(char message[])
while (1) {
memset(input, 0, MAX_BUFFER_LENGTH);
while (xgets(input, MAX_BUFFER_LENGTH)) {

/* turn off pmdc control */
if (!strncmp(input, "quit", 4)) {
// deinit DAC GPIOB GPIOD
DAC_Reset();
Expand All @@ -331,6 +333,17 @@ void command_pmdc(char message[])
xprintf("Stop to control motor ...\n\r");
return;
}

/* change the speed */
uint32_t new_speed = 0;
char *pInput = input;
if(xatoi(&pInput, (int32_t *)&new_speed)){
if ((new_speed > MAX_VOLT) || (new_speed < 0))
xprintf("Invaliad input, volt should be in 0 ~ %d\n\r", MAX_VOLT);
else{
DAC_Output_value = new_speed;
}
}
}
}
}
Expand Down

0 comments on commit 7975136

Please sign in to comment.