2014-01-09 06:20:26 -08:00
|
|
|
/*
|
|
|
|
Copyright 2012-2014 Benjamin Vedder benjamin@vedder.se
|
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* terminal.c
|
|
|
|
*
|
|
|
|
* Created on: 26 dec 2013
|
|
|
|
* Author: benjamin
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "ch.h"
|
|
|
|
#include "hal.h"
|
|
|
|
#include "terminal.h"
|
2014-09-19 18:22:38 -07:00
|
|
|
#include "commands.h"
|
2014-01-09 06:20:26 -08:00
|
|
|
#include "main.h"
|
2014-08-13 15:23:32 -07:00
|
|
|
#include "hw.h"
|
2015-02-19 12:20:07 -08:00
|
|
|
#include "comm_can.h"
|
|
|
|
#include "utils.h"
|
2014-01-09 06:20:26 -08:00
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
2014-10-24 14:04:10 -07:00
|
|
|
// Private variables
|
2015-08-23 09:26:05 -07:00
|
|
|
#define FAULT_VEC_LEN 25
|
2014-10-24 14:04:10 -07:00
|
|
|
static volatile fault_data fault_vec[FAULT_VEC_LEN];
|
|
|
|
static volatile int fault_vec_write = 0;
|
|
|
|
|
2014-01-09 06:20:26 -08:00
|
|
|
void terminal_process_string(char *str) {
|
|
|
|
enum { kMaxArgs = 64 };
|
|
|
|
int argc = 0;
|
|
|
|
char *argv[kMaxArgs];
|
|
|
|
|
|
|
|
char *p2 = strtok(str, " ");
|
|
|
|
while (p2 && argc < kMaxArgs) {
|
|
|
|
argv[argc++] = p2;
|
|
|
|
p2 = strtok(0, " ");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (argc == 0) {
|
2014-09-19 18:22:38 -07:00
|
|
|
commands_printf("No command received\n");
|
2014-01-09 06:20:26 -08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strcmp(argv[0], "ping") == 0) {
|
2014-09-19 18:22:38 -07:00
|
|
|
commands_printf("pong\n");
|
2014-01-09 06:20:26 -08:00
|
|
|
} else if (strcmp(argv[0], "stop") == 0) {
|
|
|
|
mcpwm_set_duty(0);
|
2014-09-19 18:22:38 -07:00
|
|
|
commands_printf("Motor stopped\n");
|
2014-01-09 06:20:26 -08:00
|
|
|
} else if (strcmp(argv[0], "last_adc_duration") == 0) {
|
2014-09-19 18:22:38 -07:00
|
|
|
commands_printf("Latest ADC duration: %.4f ms", (double)(mcpwm_get_last_adc_isr_duration() * 1000.0));
|
|
|
|
commands_printf("Latest injected ADC duration: %.4f ms", (double)(mcpwm_get_last_inj_adc_isr_duration() * 1000.0));
|
|
|
|
commands_printf("Latest main ADC duration: %.4f ms\n", (double)(main_get_last_adc_isr_duration() * 1000.0));
|
2014-01-09 06:20:26 -08:00
|
|
|
} else if (strcmp(argv[0], "kv") == 0) {
|
2014-09-19 18:22:38 -07:00
|
|
|
commands_printf("Calculated KV: %.2f rpm/volt\n", (double)mcpwm_get_kv_filtered());
|
2014-01-09 06:20:26 -08:00
|
|
|
} else if (strcmp(argv[0], "mem") == 0) {
|
|
|
|
size_t n, size;
|
|
|
|
n = chHeapStatus(NULL, &size);
|
2014-09-19 18:22:38 -07:00
|
|
|
commands_printf("core free memory : %u bytes", chCoreStatus());
|
|
|
|
commands_printf("heap fragments : %u", n);
|
|
|
|
commands_printf("heap free total : %u bytes\n", size);
|
2014-01-09 06:20:26 -08:00
|
|
|
} else if (strcmp(argv[0], "threads") == 0) {
|
|
|
|
Thread *tp;
|
|
|
|
static const char *states[] = {THD_STATE_NAMES};
|
2014-09-19 18:22:38 -07:00
|
|
|
commands_printf(" addr stack prio refs state name time ");
|
|
|
|
commands_printf("-------------------------------------------------------------");
|
2014-01-09 06:20:26 -08:00
|
|
|
tp = chRegFirstThread();
|
|
|
|
do {
|
2014-09-19 18:22:38 -07:00
|
|
|
commands_printf("%.8lx %.8lx %4lu %4lu %9s %14s %lu",
|
2014-01-09 06:20:26 -08:00
|
|
|
(uint32_t)tp, (uint32_t)tp->p_ctx.r13,
|
|
|
|
(uint32_t)tp->p_prio, (uint32_t)(tp->p_refs - 1),
|
|
|
|
states[tp->p_state], tp->p_name, (uint32_t)tp->p_time);
|
|
|
|
tp = chRegNextThread(tp);
|
|
|
|
} while (tp != NULL);
|
2014-09-19 18:22:38 -07:00
|
|
|
commands_printf("");
|
2014-03-23 14:31:57 -07:00
|
|
|
} else if (strcmp(argv[0], "fault") == 0) {
|
2014-10-24 14:04:10 -07:00
|
|
|
commands_printf("%s\n", mcpwm_fault_to_string(mcpwm_get_fault()));
|
|
|
|
} else if (strcmp(argv[0], "faults") == 0) {
|
|
|
|
if (fault_vec_write == 0) {
|
|
|
|
commands_printf("No faults registered since startup\n");
|
|
|
|
} else {
|
|
|
|
commands_printf("The following faults were registered since start:\n");
|
|
|
|
for (int i = 0;i < fault_vec_write;i++) {
|
|
|
|
commands_printf("Fault : %s", mcpwm_fault_to_string(fault_vec[i].fault));
|
|
|
|
commands_printf("Current : %.1f", (double)fault_vec[i].current);
|
|
|
|
commands_printf("Current filtered : %.1f", (double)fault_vec[i].current_filtered);
|
2014-11-27 11:56:17 -08:00
|
|
|
commands_printf("Voltage : %.2f", (double)fault_vec[i].voltage);
|
2014-10-24 14:04:10 -07:00
|
|
|
commands_printf("Duty : %.2f", (double)fault_vec[i].duty);
|
|
|
|
commands_printf("RPM : %.1f", (double)fault_vec[i].rpm);
|
|
|
|
commands_printf("Tacho : %d", fault_vec[i].tacho);
|
2015-08-23 09:26:05 -07:00
|
|
|
commands_printf("Cycles running : %d", fault_vec[i].cycles_running);
|
2015-08-26 14:12:39 -07:00
|
|
|
commands_printf("PWM cycles : %d", fault_vec[i].pwm_cycles);
|
2015-08-23 09:26:05 -07:00
|
|
|
commands_printf("TIM duty : %d", (int)((float)fault_vec[i].tim_top * fault_vec[i].duty));
|
|
|
|
commands_printf("TIM val samp : %d", fault_vec[i].tim_val_samp);
|
|
|
|
commands_printf("TIM current samp : %d", fault_vec[i].tim_current_samp);
|
|
|
|
commands_printf("TIM top : %d", fault_vec[i].tim_top);
|
2014-10-24 14:04:10 -07:00
|
|
|
commands_printf("Comm step : %d", fault_vec[i].comm_step);
|
|
|
|
commands_printf("Temperature : %.2f\n", (double)fault_vec[i].temperature);
|
|
|
|
}
|
|
|
|
}
|
2014-03-29 16:57:22 -07:00
|
|
|
} else if (strcmp(argv[0], "rpm") == 0) {
|
2014-09-19 18:22:38 -07:00
|
|
|
commands_printf("Electrical RPM: %.2f rpm\n", (double)mcpwm_get_rpm());
|
2014-03-29 16:57:22 -07:00
|
|
|
} else if (strcmp(argv[0], "tacho") == 0) {
|
2014-09-19 18:22:38 -07:00
|
|
|
commands_printf("Tachometer counts: %i\n", mcpwm_get_tachometer_value(0));
|
2014-04-01 10:26:46 -07:00
|
|
|
} else if (strcmp(argv[0], "tim") == 0) {
|
2014-09-12 04:03:34 -07:00
|
|
|
chSysLock();
|
|
|
|
volatile int t1_cnt = TIM1->CNT;
|
|
|
|
volatile int t8_cnt = TIM8->CNT;
|
|
|
|
chSysUnlock();
|
2014-04-01 10:26:46 -07:00
|
|
|
int duty = TIM1->CCR1;
|
|
|
|
int top = TIM1->ARR;
|
|
|
|
int voltage_samp = TIM8->CCR1;
|
|
|
|
int current1_samp = TIM1->CCR4;
|
|
|
|
int current2_samp = TIM8->CCR2;
|
2014-09-19 18:22:38 -07:00
|
|
|
commands_printf("Tim1 CNT: %i", t1_cnt);
|
|
|
|
commands_printf("Tim8 CNT: %u", t8_cnt);
|
|
|
|
commands_printf("Duty cycle: %u", duty);
|
|
|
|
commands_printf("Top: %u", top);
|
|
|
|
commands_printf("Voltage sample: %u", voltage_samp);
|
|
|
|
commands_printf("Current 1 sample: %u", current1_samp);
|
|
|
|
commands_printf("Current 2 sample: %u\n", current2_samp);
|
2014-08-13 15:23:32 -07:00
|
|
|
} else if (strcmp(argv[0], "volt") == 0) {
|
2014-09-19 18:22:38 -07:00
|
|
|
commands_printf("Input voltage: %.2f\n", (double)GET_INPUT_VOLTAGE());
|
2014-09-12 14:37:33 -07:00
|
|
|
} else if (strcmp(argv[0], "param_detect") == 0) {
|
|
|
|
// Use COMM_MODE_DELAY and try to figure out the motor parameters.
|
|
|
|
if (argc == 4) {
|
|
|
|
float current = -1.0;
|
|
|
|
float min_rpm = -1.0;
|
|
|
|
float low_duty = -1.0;
|
|
|
|
sscanf(argv[1], "%f", ¤t);
|
|
|
|
sscanf(argv[2], "%f", &min_rpm);
|
|
|
|
sscanf(argv[3], "%f", &low_duty);
|
|
|
|
|
2014-09-19 18:22:38 -07:00
|
|
|
const volatile mc_configuration *mcconf = mcpwm_get_configuration();
|
2014-09-14 14:08:22 -07:00
|
|
|
|
2014-09-15 13:44:39 -07:00
|
|
|
if (current > 0.0 && current < mcconf->l_current_max &&
|
2014-09-12 14:37:33 -07:00
|
|
|
min_rpm > 10.0 && min_rpm < 3000.0 &&
|
|
|
|
low_duty > 0.02 && low_duty < 0.8) {
|
|
|
|
|
2014-09-15 11:48:46 -07:00
|
|
|
float cycle_integrator;
|
|
|
|
float coupling_k;
|
2015-06-19 14:38:46 -07:00
|
|
|
int8_t hall_table[8];
|
|
|
|
int hall_res;
|
|
|
|
if (conf_general_detect_motor_param(current, min_rpm, low_duty, &cycle_integrator, &coupling_k, hall_table, &hall_res)) {
|
2014-09-19 18:22:38 -07:00
|
|
|
commands_printf("Cycle integrator limit: %.2f", (double)cycle_integrator);
|
2015-06-19 14:38:46 -07:00
|
|
|
commands_printf("Coupling factor: %.2f", (double)coupling_k);
|
|
|
|
|
|
|
|
if (hall_res == 0) {
|
|
|
|
commands_printf("Detected hall sensor table:");
|
|
|
|
commands_printf("%i, %i, %i, %i, %i, %i, %i, %i\n",
|
|
|
|
hall_table[0], hall_table[1], hall_table[2], hall_table[3],
|
|
|
|
hall_table[4], hall_table[5], hall_table[6], hall_table[7]);
|
|
|
|
} else if (hall_res == -1) {
|
|
|
|
commands_printf("Hall sensor detection failed:");
|
|
|
|
commands_printf("%i, %i, %i, %i, %i, %i, %i, %i\n",
|
|
|
|
hall_table[0], hall_table[1], hall_table[2], hall_table[3],
|
|
|
|
hall_table[4], hall_table[5], hall_table[6], hall_table[7]);
|
|
|
|
} else if (hall_res == -2) {
|
|
|
|
commands_printf("WS2811 enabled. Hall sensors cannot be used.\n");
|
|
|
|
} else if (hall_res == -3) {
|
|
|
|
commands_printf("Encoder enabled. Hall sensors cannot be used.\n");
|
|
|
|
}
|
2014-09-15 11:48:46 -07:00
|
|
|
} else {
|
2014-09-19 18:22:38 -07:00
|
|
|
commands_printf("Detection failed. Try again with different parameters.\n");
|
2014-09-12 14:37:33 -07:00
|
|
|
}
|
|
|
|
} else {
|
2014-09-19 18:22:38 -07:00
|
|
|
commands_printf("Invalid argument(s).\n");
|
2014-09-12 14:37:33 -07:00
|
|
|
}
|
|
|
|
} else {
|
2014-09-19 18:22:38 -07:00
|
|
|
commands_printf("This command requires three arguments.\n");
|
2014-09-12 14:37:33 -07:00
|
|
|
}
|
2014-09-13 01:46:46 -07:00
|
|
|
} else if (strcmp(argv[0], "rpm_dep") == 0) {
|
|
|
|
mc_rpm_dep_struct rpm_dep = mcpwm_get_rpm_dep();
|
2014-09-19 18:22:38 -07:00
|
|
|
commands_printf("Cycle int limit: %.2f", (double)rpm_dep.cycle_int_limit);
|
|
|
|
commands_printf("Cycle int limit running: %.2f", (double)rpm_dep.cycle_int_limit_running);
|
|
|
|
commands_printf("Cycle int limit max: %.2f\n", (double)rpm_dep.cycle_int_limit_max);
|
2015-02-19 12:20:07 -08:00
|
|
|
} else if (strcmp(argv[0], "can_devs") == 0) {
|
|
|
|
commands_printf("CAN devices seen on the bus the past second:\n");
|
|
|
|
for (int i = 0;i < CAN_STATUS_MSGS_TO_STORE;i++) {
|
|
|
|
can_status_msg *msg = comm_can_get_status_msg_index(i);
|
|
|
|
|
|
|
|
if (msg->id >= 0 && UTILS_AGE_S(msg->rx_time) < 1.0) {
|
|
|
|
commands_printf("ID : %i", msg->id);
|
|
|
|
commands_printf("RX Time : %i", msg->rx_time);
|
|
|
|
commands_printf("Age (milliseconds) : %.2f", (double)(UTILS_AGE_S(msg->rx_time) * 1000.0));
|
|
|
|
commands_printf("RPM : %.2f", (double)msg->rpm);
|
|
|
|
commands_printf("Current : %.2f", (double)msg->current);
|
|
|
|
commands_printf("Duty : %.2f\n", (double)msg->duty);
|
|
|
|
}
|
|
|
|
}
|
2014-06-29 05:23:39 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// The help command
|
|
|
|
else if (strcmp(argv[0], "help") == 0) {
|
2014-09-19 18:22:38 -07:00
|
|
|
commands_printf("Valid commands are:");
|
|
|
|
commands_printf("help");
|
|
|
|
commands_printf(" Show this help");
|
2014-01-09 06:20:26 -08:00
|
|
|
|
2014-09-19 18:22:38 -07:00
|
|
|
commands_printf("ping");
|
|
|
|
commands_printf(" Print pong here to see if the reply works");
|
2014-01-09 06:20:26 -08:00
|
|
|
|
2014-09-19 18:22:38 -07:00
|
|
|
commands_printf("stop");
|
|
|
|
commands_printf(" Stop the motor");
|
2014-01-09 06:20:26 -08:00
|
|
|
|
2014-09-19 18:22:38 -07:00
|
|
|
commands_printf("last_adc_duration");
|
|
|
|
commands_printf(" The time the latest ADC interrupt consumed");
|
2014-01-09 06:20:26 -08:00
|
|
|
|
2014-09-19 18:22:38 -07:00
|
|
|
commands_printf("kv");
|
|
|
|
commands_printf(" The calculated kv of the motor");
|
2014-01-09 06:20:26 -08:00
|
|
|
|
2014-09-19 18:22:38 -07:00
|
|
|
commands_printf("mem");
|
|
|
|
commands_printf(" Show memory usage");
|
2014-01-09 06:20:26 -08:00
|
|
|
|
2014-09-19 18:22:38 -07:00
|
|
|
commands_printf("threads");
|
|
|
|
commands_printf(" List all threads");
|
2014-03-23 14:31:57 -07:00
|
|
|
|
2014-09-19 18:22:38 -07:00
|
|
|
commands_printf("fault");
|
|
|
|
commands_printf(" Prints the current fault code");
|
2014-03-29 16:57:22 -07:00
|
|
|
|
2014-10-24 14:04:10 -07:00
|
|
|
commands_printf("faults");
|
|
|
|
commands_printf(" Prints all stored fault codes and conditions when they arrived");
|
|
|
|
|
2014-09-19 18:22:38 -07:00
|
|
|
commands_printf("rpm");
|
|
|
|
commands_printf(" Prints the current electrical RPM");
|
2014-03-29 16:57:22 -07:00
|
|
|
|
2014-09-19 18:22:38 -07:00
|
|
|
commands_printf("tacho");
|
|
|
|
commands_printf(" Prints tachometer value");
|
2014-04-01 10:26:46 -07:00
|
|
|
|
2014-09-19 18:22:38 -07:00
|
|
|
commands_printf("tim");
|
|
|
|
commands_printf(" Prints tim1 and tim8 settings");
|
2014-06-29 05:23:39 -07:00
|
|
|
|
2014-09-19 18:22:38 -07:00
|
|
|
commands_printf("volt");
|
|
|
|
commands_printf(" Prints different voltages");
|
2014-09-12 14:37:33 -07:00
|
|
|
|
2014-09-19 18:22:38 -07:00
|
|
|
commands_printf("param_detect [current] [min_rpm] [low_duty]");
|
|
|
|
commands_printf(" Spin up the motor in COMM_MODE_DELAY and compute its parameters.");
|
|
|
|
commands_printf(" This test should be performed without load on the motor.");
|
|
|
|
commands_printf(" Example: param_detect 5.0 600 0.06");
|
2014-09-13 01:46:46 -07:00
|
|
|
|
2014-09-19 18:22:38 -07:00
|
|
|
commands_printf("rpm_dep");
|
2015-02-19 12:20:07 -08:00
|
|
|
commands_printf(" Prints some rpm-dep values");
|
|
|
|
|
|
|
|
commands_printf("can_devs");
|
|
|
|
commands_printf(" Prints all CAN devices seen on the bus the past second\n");
|
2014-01-09 06:20:26 -08:00
|
|
|
} else {
|
2014-09-19 18:22:38 -07:00
|
|
|
commands_printf("Invalid command: %s\n"
|
2014-01-09 06:20:26 -08:00
|
|
|
"type help to list all available commands\n", argv[0]);
|
|
|
|
}
|
|
|
|
}
|
2014-10-24 14:04:10 -07:00
|
|
|
|
|
|
|
void terminal_add_fault_data(fault_data *data) {
|
|
|
|
fault_vec[fault_vec_write++] = *data;
|
|
|
|
if (fault_vec_write >= FAULT_VEC_LEN) {
|
|
|
|
fault_vec_write = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|