Sei sulla pagina 1di 13

#include <avr/io.

h>

#include "include/Servo.h"

#include "include/Options.h"

/*

** constants/macros

*/

#define DDR(x) (*(&x - 1)) /* address of data direction register of port x */

#if defined(__AVR_ATmega64__) || defined(__AVR_ATmega128__)

/* on ATmega64/128 PINF is on port 0x00 and not 0x60 */

#define PIN(x) ( &PORTF==&(x) ? _SFR_IO8(0x00) : (*(&x - 2)) )

#else

#define PIN(x) (*(&x - 2)) /* address of input register of port x */

#endif

volatile unsigned char _cmdIndex=0;

void DataExchange::SendPackage()

ARXTX.TXBuf[0]='R';

ARXTX.TXBuf[1]='G';

ARXTX.TXBuf[2]=BionicArm.MYADC[0];

ARXTX.TXBuf[3]=BionicArm.MYADC[1];

ARXTX.TXBuf[4]=BionicArm.bytesMAV[0];

ARXTX.TXBuf[5]=BionicArm.bytesMAV[1];

ARXTX.TXIndex=0;

ARXTX.DataInTxBuf=1;//Данные в передатчике
UDR=ARXTX.TXBuf[0];

return;

void DataExchange::ExchangeDataTask()

if(ARXTX.DataInRxBuf)//Если получен еще один байт

ARXTX.DataInRxBuf=0;

//if (CheckCRC(ARXTX.RXBuf,ARXTX.RXPLength))//Проверяем контрольную сумму

if(ARXTX.CheckCRC())

//BoardA.FlashLed2();

//BoardA.SetSpeed(ARXTX.RXBuf[1]);

/*

if(ARXTX.RXBuf[0]==BUIMP)//Проверяем адресc

switch (ARXTX.RXBuf[1])//Отрабатываем соответствующую комманду

case PressKey:

remKeys.KeyPressCode=ARXTX.RXBuf[2];

break;

case 0x01:
ARXTX.ParamBuf[0]=(memDNP_1[0] & 0x0f)|
((memDNP_1[1]&0x0f)<<4);

ARXTX.ParamBuf[1]=(memDNP_1[2] & 0x0f)|


((memDNP_1[3] & 0x0f)<<4);

ARXTX.ParamBuf[2]=(memDNP_1[4] & 0x0f);

SendPackage(0,BUIMP,0x01);

default:

break;

*/

return;

extern _Servo Servos;

_Servo::_Servo()

unsigned char i;

ServoPinsInit();

KeyInterruptInit();
for(i=0;i<5;i++){Servo[i][Active]=80;Servo[i][Desired]=230;}

return;

void _Servo::KeyInterruptInit()

TCCR0A=(1<<CS00);

OCR0A=125;

TIMSK0|=(1<<OCIE0A);

return;

void _Servo::HandlerInterrupt()

TCNT0=0x00;

if(Servos.Servo[0][Active]>Servos.Counter)_0Servo_PORT |=_BV(_0Servo_PIN); else


_0Servo_PORT &=~_BV(_0Servo_PIN);

if(Servos.Servo[1][Active]>Servos.Counter)_1Servo_PORT |=_BV(_1Servo_PIN); else


_1Servo_PORT &=~_BV(_1Servo_PIN);

if(Servos.Servo[2][Active]>Servos.Counter)_2Servo_PORT |=_BV(_2Servo_PIN); else


_2Servo_PORT &=~_BV(_2Servo_PIN);

if(Servos.Servo[3][Active]>Servos.Counter)_3Servo_PORT |=_BV(_3Servo_PIN); else


_3Servo_PORT &=~_BV(_3Servo_PIN);

if(Servos.Servo[4][Active]>Servos.Counter)_4Servo_PORT |=_BV(_4Servo_PIN); else


_4Servo_PORT &=~_BV(_4Servo_PIN);

Servos.Counter++;

if(Servos.Counter==1695)Servos.Counter=0;

return;

}
void _Servo::ServoPinsInit()

DDR(_0Servo_PORT) |= _BV(_0Servo_PIN);

_0Servo_PORT &= ~_BV(_0Servo_PIN);

DDR(_1Servo_PORT) |= _BV(_1Servo_PIN);

_1Servo_PORT &= ~_BV(_1Servo_PIN);

DDR(_2Servo_PORT) |= _BV(_2Servo_PIN);

_2Servo_PORT &= ~_BV(_2Servo_PIN);

DDR(_3Servo_PORT) |= _BV(_3Servo_PIN);

_3Servo_PORT &= ~_BV(_3Servo_PIN);

DDR(_4Servo_PORT) |= _BV(_4Servo_PIN);

_4Servo_PORT &= ~_BV(_4Servo_PIN);

return;

/*

* BionicArm.cpp

* Created: 23.02.2016 15:07:29

* Author: Andrew

*/

#include <avr/io.h>
#include "include/Options.h"

#include <avr/interrupt.h>

//#include "include/connect.h"

#include "include/Servo.h"

#include "include/BionicArm.h"

_Servo Servos;

_BionicArm BionicArm;

ISR(TIMER0_COMPA_vect)

Servos.HandlerInterrupt();

ISR(TIMER1_OVF_vect)

if(BionicArm.tmrCurrentToDesiredTask>0)BionicArm.tmrCurrentToDesiredTask--;

volatile unsigned int MAV[70];

volatile unsigned char MAVIndex;

volatile unsigned char index=0;

volatile unsigned char FingersArray[5][5]={{70,230,230,70,70},{90,120,150,120,120},


{100,100,130,130,130},{120,80,120,140,140},{230,70,60,230,230}};

#define UpLemitServo 230


#define DnLemitServo 70

int main(void)

unsigned char i=0;

sei();

while(1)

BionicArm.ServoCurrentToDesiredTask();

if(BionicArm.ActiveisEqualDesired){

i++;

if(i>4)i=0;

Servos.Servo[0][Desired]=FingersArray[i][0];

Servos.Servo[1][Desired]=FingersArray[i][1];

Servos.Servo[2][Desired]=FingersArray[i][2];

Servos.Servo[3][Desired]=FingersArray[i][3];

Servos.Servo[4][Desired]=FingersArray[i][4];

BionicArm.ActiveisEqualDesired=0;

#include <avr/io.h>

#include "include/Options.h"

#include "include/BionicArm.h"

#include "include/Servo.h"
//#include "include/connect.h"

/*

** constants/macros

*/

#define DDR(x) (*(&x - 1)) /* address of data direction register of port x */

#if defined(__AVR_ATmega64__) || defined(__AVR_ATmega128__)

/* on ATmega64/128 PINF is on port 0x00 and not 0x60 */

#define PIN(x) ( &PORTF==&(x) ? _SFR_IO8(0x00) : (*(&x - 2)) )

#else

#define PIN(x) (*(&x - 2)) /* address of input register of port x */

#endif

extern _Servo Servos;

extern _BionicArm BionicArm;

//extern DataExchange ARXTX;

_BionicArm::_BionicArm()

KeyInterruptInit();

return;

void _BionicArm::initLeds()

DDR(_Led1_PORT) |= _BV(_Led1_PIN);

_Led1_PORT &= ~_BV(_Led1_PIN);

DDR(_Led2_PORT) |= _BV(_Led2_PIN);
_Led2_PORT &= ~_BV(_Led2_PIN);

DDR(_Led3_PORT) |= _BV(_Led3_PIN);

_Led3_PORT &= ~_BV(_Led3_PIN);

DDR(_Led4_PORT) |= _BV(_Led4_PIN);

_Led4_PORT &= ~_BV(_Led4_PIN);

DDR(_Led5_PORT) |= _BV(_Led5_PIN);

_Led5_PORT &= ~_BV(_Led5_PIN);

return;

void _BionicArm::SetLed(unsigned char index)

switch (index)

case 0:

_Led1_PORT |= _BV(_Led1_PIN);

_Led2_PORT &=~ _BV(_Led2_PIN);

_Led3_PORT &=~_BV(_Led3_PIN);

_Led4_PORT &=~ _BV(_Led4_PIN);

_Led5_PORT &=~ _BV(_Led5_PIN);

break;

case 1:

_Led1_PORT &=~ _BV(_Led1_PIN);


_Led2_PORT |= _BV(_Led2_PIN);

_Led3_PORT &=~ _BV(_Led3_PIN);

_Led4_PORT &=~ _BV(_Led4_PIN);

_Led5_PORT &=~ _BV(_Led5_PIN);

break;

case 2:

_Led1_PORT &=~ _BV(_Led1_PIN);

_Led2_PORT &=~ _BV(_Led2_PIN);

_Led3_PORT |= _BV(_Led3_PIN);

_Led4_PORT &=~ _BV(_Led4_PIN);

_Led5_PORT &=~ _BV(_Led5_PIN);

break;

case 3:

_Led1_PORT &=~ _BV(_Led1_PIN);

_Led2_PORT &=~ _BV(_Led2_PIN);

_Led3_PORT &=~ _BV(_Led3_PIN);

_Led4_PORT |= _BV(_Led4_PIN);

_Led5_PORT &=~ _BV(_Led5_PIN);

break;

case 4:

_Led1_PORT &=~ _BV(_Led1_PIN);

_Led2_PORT &=~ _BV(_Led2_PIN);

_Led3_PORT &=~ _BV(_Led3_PIN);

_Led4_PORT &=~ _BV(_Led4_PIN);

_Led5_PORT |= _BV(_Led5_PIN);

break;
}

void _BionicArm::initButton()

DDR(Button_PORT)&= _BV(Button_PIN);

Button_PORT |= ~_BV(Button_PIN);

return;

bool _BionicArm::getButtonState()

return PIN(Button_PORT) & _BV(Button_PIN);

#define UpLemitServo 230

#define DnLemitServo 70

void _BionicArm:: ServoCurrentToDesiredTask()

if(tmrCurrentToDesiredTask) return;

if(Servos.Servo[0][Active]<Servos.Servo[0][Desired] && Servos.Servo[0][Active]<UpLemitServo)


{Servos.Servo[0][Active]++;}

if(Servos.Servo[1][Active]<Servos.Servo[1][Desired] && Servos.Servo[1][Active]<UpLemitServo)


{Servos.Servo[1][Active]++;}

if(Servos.Servo[2][Active]<Servos.Servo[2][Desired] && Servos.Servo[2][Active]<UpLemitServo)


{Servos.Servo[2][Active]++;}

if(Servos.Servo[3][Active]<Servos.Servo[3][Desired] && Servos.Servo[3][Active]<UpLemitServo)


{Servos.Servo[3][Active]++;}
if(Servos.Servo[4][Active]<Servos.Servo[4][Desired] && Servos.Servo[4][Active]<UpLemitServo)
{Servos.Servo[4][Active]++;}

if(Servos.Servo[0][Active]>Servos.Servo[0][Desired] && Servos.Servo[0][Active]>DnLemitServo)


{Servos.Servo[0][Active]--;}

if(Servos.Servo[1][Active]>Servos.Servo[1][Desired] && Servos.Servo[1][Active]>DnLemitServo)


{Servos.Servo[1][Active]--;}

if(Servos.Servo[2][Active]>Servos.Servo[2][Desired] && Servos.Servo[2][Active]>DnLemitServo)


{Servos.Servo[2][Active]--;}

if(Servos.Servo[3][Active]>Servos.Servo[3][Desired] && Servos.Servo[3][Active]>DnLemitServo)


{Servos.Servo[3][Active]--;}

if(Servos.Servo[4][Active]>Servos.Servo[4][Desired] && Servos.Servo[4][Active]>DnLemitServo)


{Servos.Servo[4][Active]--;}

/*

if(Servos.Servo[0][Active]==Servos.Servo[0][Desired] &&

Servos.Servo[1][Active]==Servos.Servo[1][Desired] &&

Servos.Servo[2][Active]==Servos.Servo[2][Desired] &&

Servos.Servo[3][Active]==Servos.Servo[3][Desired] &&

Servos.Servo[4][Active]==Servos.Servo[4][Desired]

)BionicArm.ActiveisEqualDesired=1;else BionicArm.ActiveisEqualDesired=0;

*/

tmrCurrentToDesiredTask=_constCurrentToDesiredTask;

return;

void _BionicArm::KeyInterruptInit()

//TMR1

TCCR1A=0x00;

TCCR1B=(1<<CS11);
//TMR2

//TCCR2=(1<<CS22)|(1<<CS21);

TIMSK0|=(1<<TOIE1)|(1<<TOIE2);

return;

void _BionicArm::TurnOnAllLeds()

_Led1_PORT |= _BV(_Led1_PIN);

_Led2_PORT |= _BV(_Led2_PIN);

_Led3_PORT |= _BV(_Led3_PIN);

_Led4_PORT |= _BV(_Led4_PIN);

_Led5_PORT |= _BV(_Led5_PIN);

return;

void _BionicArm::TurnOffAllLeds()

_Led1_PORT &= ~_BV(_Led1_PIN);

_Led2_PORT &= ~_BV(_Led2_PIN);

_Led3_PORT &= ~_BV(_Led3_PIN);

_Led4_PORT &= ~_BV(_Led4_PIN);

_Led5_PORT &= ~ _BV(_Led5_PIN);

return;

Potrebbero piacerti anche