0% found this document useful (0 votes)
72 views

Anexo C Programacion

This document contains code for a program that uses a joystick controller to control motors. It defines pin connections for left and right motors. It initializes the motor controllers and joystick parser. The joystick parser detects button presses and joystick movement, maps the values, and uses those values to control the speed and direction of each motor appropriately to mimic the joystick movement.

Uploaded by

andres
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
72 views

Anexo C Programacion

This document contains code for a program that uses a joystick controller to control motors. It defines pin connections for left and right motors. It initializes the motor controllers and joystick parser. The joystick parser detects button presses and joystick movement, maps the values, and uses those values to control the speed and direction of each motor appropriately to mimic the joystick movement.

Uploaded by

andres
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

PROGRAMA GENERAL RECONOCIMIENTO DE CONTROL DE MANDO.

#include <usbhid.h>

#include <hiduniversal.h>

#include <usbhub.h>

#include <SPI.h>

#include "hidjoystickrptparser.h"

#ifdef dobogusinclude

#include <spi4teensy3.h>

#endif

USB Usb;

USBHub Hub(&Usb);

HIDUniversal Hid(&Usb);

JoystickEvents JoyEvents;

JoystickReportParser Joy(&JoyEvents);

void setup()

Serial.begin(115200);

#if !defined(__MIPSEL__)

while (!Serial);

#endif

Serial.println("Start");

if (Usb.Init() == -1)

Serial.println("OSC did not start.");

delay(200);
if (!Hid.SetReportParser(0, &Joy))

ErrorMessage<uint8_t > (PSTR("SetReportParser"), 1);

void loop()

Usb.Task();

#include "hidjoystickrptparser.h"

#include <RobojaxBTS7960.h>

//MOTOR IZQUIERDA

#define RPWM 3

#define R_EN 14

#define R_IS 16

#define LPWM 5

#define L_EN 15

#define L_IS 17

//MOTOR DERECHA

#define RPWM_D 2

#define R_EN_D 18

#define R_IS_D 20

#define LPWM_D 4

#define L_EN_D 19

#define L_IS_D 21

#define CW 1

#define CCW 0
#define debug 0

#define LED 52

int Valor=0,ValX=0;

byte Valor2=0,Valor3=0;

RobojaxBTS7960 motorI(R_EN,RPWM,R_IS, L_EN,LPWM,L_IS,debug);

RobojaxBTS7960 motorD(R_EN_D,RPWM_D,R_IS_D, L_EN_D,LPWM_D,L_IS_D,debug);

JoystickReportParser::JoystickReportParser(JoystickEvents *evt) :

joyEvents(evt),

oldHat(0xDE),

oldButtons(0) {

for (uint8_t i = 0; i < RPT_GEMEPAD_LEN; i++)

oldPad[i] = 0xD;

void JoystickReportParser::Parse(USBHID *hid, bool is_rpt_id, uint8_t len, uint8_t *buf)

bool match = true;

// Checking if there are changes in report since the method was last called

for (uint8_t i = 0; i < RPT_GEMEPAD_LEN; i++)

if (buf[i] != oldPad[i]) {

match = false;

break;

// Calling Game Pad event handler

if (!match && joyEvents) {


joyEvents->OnGamePadChanged((const GamePadEventData*)buf);

for (uint8_t i = 0; i < RPT_GEMEPAD_LEN; i++) oldPad[i] = buf[i];

uint8_t hat = (buf[5] & 0xF);

// Calling Hat Switch event handler

if (hat != oldHat && joyEvents) {

joyEvents->OnHatSwitch(hat);

oldHat = hat;

uint16_t buttons = (0x0000 | buf[6]);

buttons <<= 4;

buttons |= (buf[5] >> 4);

uint16_t changes = (buttons ^ oldButtons);

if (changes) {

for (uint8_t i = 0; i < 0x0C; i++) {

uint16_t mask = (0x0001 << i);

if (((mask & changes) > 0) && joyEvents) {

if ((buttons & mask) > 0)

joyEvents->OnButtonDn(i + 1);

else

joyEvents->OnButtonUp(i + 1);

}
}

oldButtons = buttons;

void JoystickEvents::OnGamePadChanged(const GamePadEventData *evt)

motorI.begin();

motorD.begin();

Serial.print("X1: ");

Serial.println(evt->X);

Serial.print("Y1: ");

Serial.println(evt->Y);

Valor=map(evt->Y,0,255,255,-255);

ValX=map(evt->X,0,255,255,-255);

if(Valor<0)

Valor2=map(Valor,1,-255,0,255);

if(Valor<-20)

motorI.rotate(30,CCW);

motorD.rotate(30,CCW);

if(Valor>0)

Valor2=map(Valor,1,255,0,255); // ADELANTE
if(Valor>20)

motorI.rotate(30,CW); // motorI.rotate(Valor2,CW)

motorD.rotate(30,CW); // motorD.rotate(Valor2,CW);

// Serial.println(Valor2); SI QUIERO VER EL VALOR

if(evt->X<40)

// Valor3=map(ValX,1,-255,0,255); //IZQUIERDA

motorI.rotate(30,CCW);

motorD.rotate(30,CW);

// Serial.println(Valor3);

if(evt->X>140)

// Valor3=map(ValX,1,255,0,255); //DERECHA

motorI.rotate(30,CW);

motorD.rotate(30,CCW);

// Serial.println(Valor3);

if(((evt->X<140)&&(evt->X>118))&&((evt->Y<140)&&(evt->Y>118)))

motorI.stop();

motorD.stop();

}
void JoystickEvents::OnHatSwitch(uint8_t hat)

Serial.print("Hat Switch: ");

PrintHex<uint8_t > (hat, 0x80);

Serial.println("");

void JoystickEvents::OnButtonUp(uint8_t but_id)

Serial.print("Up: ");

Serial.println(but_id, DEC);

void JoystickEvents::OnButtonDn(uint8_t but_id)

Serial.print("Dn: ");

Serial.println(but_id, DEC);

digitalWrite(LED, LOW);

#if !defined(__HIDJOYSTICKRPTPARSER_H__)

#define __HIDJOYSTICKRPTPARSER_H__

#include <usbhid.h>

struct GamePadEventData

uint8_t X, Y, Z1, Z2, Rz;


};

class JoystickEvents

public:

virtual void OnGamePadChanged(const GamePadEventData *evt);

virtual void OnHatSwitch(uint8_t hat);

virtual void OnButtonUp(uint8_t but_id);

virtual void OnButtonDn(uint8_t but_id);

};

#define RPT_GEMEPAD_LEN 5

class JoystickReportParser : public HIDReportParser {

JoystickEvents *joyEvents;

uint8_t oldPad[RPT_GEMEPAD_LEN];

uint8_t oldHat;

uint16_t oldButtons;

public:

JoystickReportParser(JoystickEvents *evt);

virtual void Parse(USBHID *hid, bool is_rpt_id, uint8_t len, uint8_t *buf);

};

#endif // __HIDJOYSTICKRPTPARSER_H__

You might also like