Browse Source

Add command_joystick command generator.

Can be used to quickly send float setpoints for testing.
beaglebone
Jeroen Vreeken 6 years ago
parent
commit
f3e2571a61
5 changed files with 162 additions and 7 deletions
  1. +10
    -2
      common/command/build.mk
  2. BIN
      common/command/command_joystick
  3. +147
    -0
      common/command/command_joystick.c
  4. +2
    -2
      controller/ec/block_stoeber.c
  5. +3
    -3
      controller/vesp/block_vesp_controller.c

+ 10
- 2
common/command/build.mk View File

@ -1,7 +1,7 @@
COMMAND_TARGETS += $(LIBDIR)/libcommand.la
COMMAND_TARGETS += $(DIR)/command_list $(DIR)/command_send
COMMAND_TARGETS += $(DIR)/command_list $(DIR)/command_send $(DIR)/command_joystick
ARCHSRCS := $(DIR)/command.c $(DIR)/command_tcp.c
@ -28,9 +28,17 @@ $(DIR)/command_send: libcommand.la
$(DIR)/command_send_LDFLAGS += -lcommand
$(DIR)/command_send: $(COMMAND_SEND_OBJS)
SRCS += $(ARCHSRCS) $(COMMAND_LIST_SRCS) $(COMMAND_SEND_SRCS)
COMMAND_JOYSTICK_SRCS := $(DIR)/command_joystick.c
COMMAND_JOYSTICK_OBJS := $(COMMAND_JOYSTICK_SRCS:.c=.o)
$(DIR)/command_joystick: libcommand.la
$(DIR)/command_joystick_LDFLAGS += -lcommand
$(DIR)/command_joystick: $(COMMAND_JOYSTICK_OBJS)
SRCS += $(ARCHSRCS) $(COMMAND_LIST_SRCS) $(COMMAND_SEND_SRCS) $(COMMAND_JOYSTICK_SRCS)
TARGETS += $(COMMAND_TARGETS)
CLEAN += $(COMMAND_TARGETS) $(ARCHOBJS) \
$(COMMAND_LIST_OBJS) \
$(COMMAND_SEND_OBJS) \
$(COMMAND_JOYSTICK_OBJS) \
$(LIBDIR)/libcommand.a

BIN
common/command/command_joystick View File


+ 147
- 0
common/command/command_joystick.c View File

@ -0,0 +1,147 @@
/*
Copyright Jeroen Vreeken (jeroen@vreeken.net), 2015
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/>.
*/
#include <stdio.h>
#include <fcntl.h>
#include <linux/joystick.h>
#include <command/command.h>
#include <dt_port_numbers.h>
#include <stdbool.h>
static bool found_name = false;
static bool found_float = false;
static bool found_setpoint = false;
static char *arg_name;
static void handler_list_entry(struct command *command,
char *name, enum command_value_type type, char *unit,
int typec, enum command_ptype typev[])
{
int i;
if (strcmp(name, arg_name))
return;
found_name = true;
found_float = (type == COMMAND_VALUE_TYPE_FLOAT);
for (i = 0; i < typec; i++) {
if (typev[i] == COMMAND_PTYPE_SETPOINT)
found_setpoint = true;
}
}
int main(int argc, char **argv)
{
struct command *command;
char *id = "command_joystick";
char *host;
short port = 21000;
char *js_dev;
int fd_joy = -1;
if (argc < 5) {
printf("Usage: %s <host> <port> <name> <joystick dev>\n", argv[0]);
printf("Example: %s localhost %d spgname /dev/input/js0", argv[0], port);
return 1;
}
host = argv[1];
port = atoi(argv[2]);
arg_name = argv[3];
js_dev = argv[4];
fd_joy = open(js_dev, O_RDONLY);
if (fd_joy < 0) {
perror("Could not open joystick device");
return 1;
}
ioctl(fd_joy, FIONBIO, &(int){ 1 });
command = command_open(host, port);
command->handler_list_entry = handler_list_entry;
while (command_state_get(command) != COMMAND_STATE_READY &&
command_state_get(command) != COMMAND_STATE_DISCONNECTED) {
fd_set fdrx;
int high = 0;
FD_ZERO(&fdrx);
command_fd_set(command, &fdrx, &high);
select(high + 1, &fdrx, NULL, NULL, NULL);
command_handle(command, &fdrx);
}
if (command_state_get(command) == COMMAND_STATE_DISCONNECTED) {
printf("Not connected.\n");
return 1;
}
if (!found_name) {
printf("Could not find spg '%s'\n", arg_name);
return 1;
}
if (!found_float) {
printf("Only float type is supported\n");
return 1;
}
if (!found_setpoint) {
printf("Setpoint command is not supported\n");
return 1;
}
command->type = COMMAND_VALUE_TYPE_FLOAT;
command_id_set(command, id);
command_name_set(command, arg_name);
do {
fd_set fdrx;
int high;
FD_ZERO(&fdrx);
FD_SET(fd_joy, &fdrx);
high = fd_joy;
command_fd_set(command, &fdrx, &high);
select(high + 1, &fdrx, NULL, NULL, NULL);
command_handle(command, &fdrx);
if (FD_ISSET(fd_joy, &fdrx)) {
struct js_event event;
ssize_t ret = read(fd_joy, &event, sizeof(event));
if (ret == sizeof(event)) {
if (event.type & JS_EVENT_AXIS && event.number == 0) {
struct command_entry entry;
entry.type = COMMAND_PTYPE_SETPOINT;
entry.value.f = (float)event.value / 32768.0;
command_send(command, &entry);
}
}
}
} while (1);
return 0;
}

+ 2
- 2
controller/ec/block_stoeber.c View File

@ -619,13 +619,13 @@ static struct controller_block *block_stoeber_create(char *name, int argc, va_li
if (!stoeber)
goto err_malloc;
sprintf(trx_name, "%s_tx", name);
sprintf(trx_name, "%s/tx", name);
stoeber_tx = controller_block_alloc("stoeber_tx", name, 0);
goto err_malloc_tx;
stoeber_tx->private = stoeber->private;
sprintf(trx_name, "%s_rx", name);
sprintf(trx_name, "%s/rx", name);
stoeber_rx = controller_block_alloc("stoeber_rx", name, 0);
if (!stoeber_rx)
goto err_malloc_rx;


+ 3
- 3
controller/vesp/block_vesp_controller.c View File

@ -192,13 +192,13 @@ static struct controller_block * block_vesp_controller_create(char *name,
if (!(controller = controller_block_alloc("vesp_controller", name, sizeof(struct controller_block_private))))
return NULL;
asprintf(&name_tmp, "%s_rx", name);
asprintf(&name_tmp, "%s/rx", name);
if (!(controller_rx = controller_block_alloc("vesp_controller_rx", name_tmp, 0)))
goto err_controller;
free(name_tmp);
controller_rx->private = controller->private;
asprintf(&name_tmp, "%s_tx", name);
asprintf(&name_tmp, "%s/tx", name);
if (!(controller_tx = controller_block_alloc("vesp_controller_tx", name_tmp, 0)))
goto err_controller;
free(name_tmp);
@ -251,7 +251,7 @@ static struct controller_block * block_vesp_controller_create(char *name,
goto err_dev;
}
if (vesp_bus_add_input(controller, controller->private->bus,
if (vesp_bus_add_input(controller_rx, controller->private->bus,
address, VESP_RESP_OOB, sizeof(struct rx_packet), rx_callback))
goto err_bus_input;


Loading…
Cancel
Save