]> rtime.felk.cvut.cz Git - pes-rpp/rpp-test-sw.git/blobdiff - rpp-test-sw/commands/cmd_hbr.c
Merge branches 'master' and 'rm48/master'
[pes-rpp/rpp-test-sw.git] / rpp-test-sw / commands / cmd_hbr.c
diff --git a/rpp-test-sw/commands/cmd_hbr.c b/rpp-test-sw/commands/cmd_hbr.c
new file mode 100644 (file)
index 0000000..1e60dce
--- /dev/null
@@ -0,0 +1,213 @@
+/*
+ * Copyright (C) 2012-2014 Czech Technical University in Prague
+ *
+ * Created on: 28.2.2013
+ *
+ * Authors:
+ *     - Michal Horn
+ *
+ * This document contains proprietary information belonging to Czech
+ * Technical University in Prague. Passing on and copying of this
+ * document, and communication of its contents is not permitted
+ * without prior written authorization.
+ *
+ * File : cmd_hbr.c
+ *
+ * Abstract:
+ *      This file contains commands controlling H-bridge
+ *
+ */
+
+#include "cmd_hbr.h"
+#include "stdio.h"
+
+#ifndef DOCGEN
+
+#include "rpp/rpp.h"
+#include "cmdproc_utils.h"
+#include "drv/drv.h"
+
+
+/**
+ * @brief      Enable HBR driver with given period of PWM
+ *
+ * @param[in]  cmd_io  Pointer to IO stack
+ * @param[in]  des             Pointer to command descriptor
+ * @param[in]  param   Parameters of command
+ * @return     0 when OK or error code
+ */
+int cmd_do_hbr_enable(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
+{
+       uint32_t period;
+       int ret;
+       char *p = param[1];
+       char spareParams;
+
+       if (sscanf(p, "%d %1s", &period, &spareParams) != 1)
+               return -CMDERR_BADPAR;
+
+       ret = rpp_hbr_enable(period);
+       if (ret == FAILURE) {
+               rpp_sci_printf("Enable procedure failed.\n");
+               return -1;
+       }
+
+       rpp_sci_printf("hbrenable =%d\n", period, ret);
+       return 0;
+}
+
+/**
+ * @brief      Control HBR driver with given value <-100; 100>
+ *
+ * @param[in]  cmd_io  Pointer to IO stack
+ * @param[in]  des             Pointer to command descriptor
+ * @param[in]  param   Parameters of command
+ * @return     0 when OK or error code
+ */
+int cmd_do_hbr_control(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
+{
+       int cmd;
+       int ret;
+       char *p = param[1];
+       char spareParams;
+
+       if (sscanf(p, "%d %1s", &cmd, &spareParams) != 1)
+               return -CMDERR_BADPAR;
+
+       ret = rpp_hbr_control((double)cmd/(double)100);
+       if (ret == -1) {
+               rpp_sci_printf("H-bridge not enabled.\n");
+               return -1;
+       }
+       else if (ret == -2) {
+               rpp_sci_printf("Command out of range.\n");
+               return -CMDERR_BADPAR;
+       }
+       rpp_sci_printf("hbrcontrol =%d\n", cmd, ret);
+       return 0;
+}
+
+/**
+ * @brief      Disable HBR driver.
+ *
+ * @param[in]  cmd_io  Pointer to IO stack
+ * @param[in]  des             Pointer to command descriptor
+ * @param[in]  param   Parameters of command
+ * @return     0 when OK or error code
+ */
+int cmd_do_hbr_disable(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
+{
+       int ret;
+
+       ret = rpp_hbr_disable();
+       if (ret == FAILURE)
+               rpp_sci_printf("H-bridge already disabled.\n");
+       return cmd_opchar_replong(cmd_io, param, ret, 0, 10);
+}
+
+#endif  /* DOCGEN */
+
+/** Command descriptor for HBR enable command */
+cmd_des_t const cmd_des_hbr_enable = {
+       0, 0,
+       "hbrenable*","Enable the H-bridge and set its PWM period",
+       "### Command syntax ###\n"
+       "\n"
+       "    hbrenable<PER>\n"
+       "\n"
+       "where `<PER>` is PWM period in microseconds.\n"
+       "\n"
+       "### Description ###\n"
+       "\n"
+       "This command enables the H-bridge (HBR pin), i.e. the enable signal\n"
+       "the H-bridge chip is set, the watchdog reset task is started and the\n"
+       "PWM is configured with the specified period and duty cycle 0%.\n"
+       "\n"
+       "If the period is zero, the default frequency of 18kHz is\n"
+       "used instead. Minimum period is 50. This command should be\n"
+       "called before any other command\n"
+       "starting with hbr is used. If H-bridge is already enabled, an error is\n"
+       "printed.\n"
+       "\n"
+       "### Example ###\n"
+       "\n"
+       "    --> hbrenable1000\n"
+       "    hbrenable =1000\n"
+       "\n"
+       "Enables HBR with period 1000 microseconds (frequency 1 kHz). HBR\n"
+       "output is still inactive, but ready for other commands.\n",
+       CMD_HANDLER(cmd_do_hbr_enable), (void *)&cmd_list_hbr
+};
+
+/** Command descriptor for HBR control command */
+cmd_des_t const cmd_des_hbr_control = {
+       0, 0,
+       "hbrcontrol*","Set the motor voltage direction and size in percent",
+       "### Command syntax ###\n"
+       "\n"
+       "    hbrcontrol<SPEED>\n"
+       "\n"
+       "where `<SPEED>` specifies direction and PWM duty cycle in percent (a\n"
+       "number in range -100, 100).\n"
+       "\n"
+       "### Description ###\n"
+       "\n"
+       "The command sets the direction and the size of the voltage at the HBR\n"
+       "output.\n"
+       "\n"
+       "HBR has to be enabled by the `hbrenable` command before calling this\n"
+       "command.\n"
+       "\n"
+       "### Examples ###\n"
+       "\n"
+       "    --> hbrcontrol-25\n"
+       "    hbrcontrol =-25\n"
+       "\n"
+       "Rotates the motor to the left with 25% duty cycle.\n"
+       "\n"
+       "    --> hbrcontrol25\n"
+       "    hbrcontrol =25\n"
+       "\n"
+       "Rotates the motor to the right with 25% duty cycle.\n"
+       "\n"
+       "    --> hbrcontrol0\n"
+       "    hbrcontrol =0\n"
+       "\n"
+       "Stops the motor, but the H-Bridge output is active (low-side active\n"
+       "free wheeling).\n",
+       CMD_HANDLER(cmd_do_hbr_control), (void *)&cmd_list_hbr
+};
+
+/** Command descriptor for HBR disable command */
+cmd_des_t const cmd_des_hbr_disable = {
+       0, 0,
+       "hbrdisable","Disable the H-bridge",
+       "### Command syntax ###\n"
+       "\n"
+       "    hbrdisable\n"
+       "\n"
+       "### Description ###\n"
+       "\n"
+       "The command disables the H-bridge HBR, which means that the PWM is\n"
+       "stopped and the enable signal is cleared. The watchdog task is left\n"
+       "running, because it is harmless.\n"
+       "\n"
+       "After the H-bridge is disabled, it cannot be controlled by any command\n"
+       "until it is enabled again by the `hbrenable` command.\n"
+       "\n"
+       "### Example ###\n"
+       "\n"
+       "    --> hbrdisable\n"
+       "    hbrdisable=0\n"
+       "\n"
+       "Stops motor and disables the H-bridge.\n",
+       CMD_HANDLER(cmd_do_hbr_disable), (void *)&cmd_list_hbr
+};
+
+/** List of commands for hbr, defined as external */
+cmd_des_t const *cmd_list_hbr[] = {
+       &cmd_des_hbr_enable,
+       &cmd_des_hbr_control,
+       &cmd_des_hbr_disable,
+       NULL
+};