You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 4 Next »

\#include <SPI.h>
\#include <SoftwareSerial.h>
\#include <CAN.h>

\#define BAUDRATE 250

//CAN muuttujat
byte candata\[8\];
byte candata_OnOff\[0\];
unsigned long id_Kaasu = 0x65;
unsigned long id_OnOff = 0x64;
byte rx_len;
byte rx_msg\[8\] = {0};
unsigned long rx_id;
byte viesti1 = 0;
byte viesti2 = 0;
byte viesti3 = 0;
byte viesti4 = 0;
byte viesti5 = 0;
byte viesti6 = 0;
byte viesti7 = 0;
byte viesti8 = 0;
byte viesti9 = 0;
byte viesti10 = 0;
byte viesti;

// laskuri CAN:lle
int laskuri;


//RS232 muuttujat
byte rx = 0;
byte tx = 1;
byte CAN_kaasu;
byte PWM_suunta = 30;
byte PWM_kauha = 30;
byte MoottoriOnOff = 0;
byte MoottoriVanha;
signed int kaasu;

int bitDelay;
int Delay;
int suuntaPin = 6;
int kauhaPin = 9;



//Ajetaan kerran Arduinon käynnistyessä
void setup()
{
&nbsp; //CAN setup
&nbsp; CAN.begin(); //CANin alustus, alustaa samalla myös SPI:n
&nbsp; CAN.baudConfig(BAUDRATE); //asettaa baudraten 125kbit/s
&nbsp; CAN.setMode(NORMAL); //asettaa CAN moden
&nbsp;
&nbsp; //datan nollaukset
&nbsp; candata\[0\] = 0x00;
&nbsp; candata\[1\] = 0x00;
&nbsp; candata\[2\] = 0x00;
&nbsp; candata\[3\] = 0x00;
&nbsp; candata\[4\] = 0x00;
&nbsp; candata\[5\] = 0x00;
&nbsp; candata\[6\] = 0x00;
&nbsp; candata\[7\] = 0x00;
&nbsp;
&nbsp; candata_OnOff\[0\] = 0x00;
&nbsp; candata_OnOff\[1\] = 0x00;
&nbsp; candata_OnOff\[2\] = 0x00;
&nbsp; candata_OnOff\[3\] = 0x00;
&nbsp; candata_OnOff\[4\] = 0x00;
&nbsp; candata_OnOff\[5\] = 0x00;
&nbsp; candata_OnOff\[6\] = 0x00;
&nbsp; candata_OnOff\[7\] = 0x00;
&nbsp;
&nbsp; Serial.begin(9600); //sarjaväylän alustus

&nbsp; pinMode(suuntaPin, OUTPUT); // pwm lähtö suunnalle outputiksi
&nbsp; pinMode(kauhaPin, OUTPUT);&nbsp; // pwm lähtö kauhalle outputiksi
&nbsp; &nbsp;
&nbsp; // sarjaväylän alustus radio modeemeille
&nbsp; pinMode(rx, INPUT);&nbsp; &nbsp;
&nbsp; pinMode(tx, OUTPUT);
&nbsp; digitalWrite(tx,HIGH);
&nbsp; bitDelay = 1000000 / 9600;
&nbsp; Delay = bitDelay - clockCyclesToMicroseconds(50); &nbsp;
&nbsp; delayMicroseconds(bitDelay);
}

void loop()
{
&nbsp; laskuri = 0;
&nbsp;
&nbsp; //Yritetään lukea moottorinohjaimen CAN-viestiä niin kauan että juuri
&nbsp; //se viesti saadaan, TAI kunnes laskuri ylittää säädetyn arvon.
&nbsp; //Laskuri tarvitaan, jottei muu toiminta jäisi jumiin jos viestiä
&nbsp; //ei jostain syystä saada EI KÄYTÃ--SSÄ\!\!
&nbsp; //do{
&nbsp;&nbsp;&nbsp; CAN.readDATA_ff_1(&rx_len, rx_msg, &rx_id); // luetaan CAN-väylää
&nbsp;&nbsp;&nbsp; laskuri++;
&nbsp;// }while(rx_id\!=0x66 && rx_id\!=167248164 && laskuri<15); //kuuluuko olla && vai \|\|
&nbsp;&nbsp; &nbsp;
&nbsp;&nbsp;&nbsp; //jos CAN viestin ID 0x66, luetaan viestin kuusi ensimmäistä alkiota muuttujiin
&nbsp; if(rx_id==0x00000066)
&nbsp; {&nbsp;&nbsp;&nbsp; &nbsp;
&nbsp;&nbsp;&nbsp; viesti1 = rx_msg\[0\]; // RPM / 8 eniten merkitsevää bittiä
&nbsp;&nbsp;&nbsp; viesti2 = rx_msg\[1\]; // RPM / 8 vähiten merkitsevää bittiä
&nbsp;&nbsp;&nbsp; viesti3 = rx_msg\[2\]; // akun jännite / 8 eniten merkitsevää bittiä &nbsp;
&nbsp;&nbsp;&nbsp; viesti4 = rx_msg\[3\]; // akun jännite / 8 vähiten merkitsevää bittiä
&nbsp;&nbsp;&nbsp; viesti5 = rx_msg\[4\]; // moottorin virta / 8 eniten merkitsevää bittiä
&nbsp;&nbsp;&nbsp; viesti6 = rx_msg\[5\]; // moottorin virta / 8 vähiten merkitsevää bittiä
&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;
&nbsp; &nbsp;&nbsp; &nbsp;// lähetetään CAN-viestin tiedot moodeemilla
&nbsp; }
&nbsp;&nbsp;&nbsp; //TÄNNE GEPSIN LUKEMINEN. LISÄÄ MYÃ--S GEPSITIETOJEN LÄHETYS (VIESTI7...)
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;//heading_radians = (unsigned int)(radian(heading)*10000);
&nbsp;&nbsp; &nbsp;//speed_metres = (unsigned int)(speed*0.5144*100);
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;//nmea2000_buffer\[0\] = (unsigned char)(fix); // position fix
&nbsp;&nbsp; &nbsp;//nmea2000_buffer\[1\] = 0xfc; // 11111100 True heading 00 + resv 6 bits
&nbsp;&nbsp; &nbsp;//nmea2000_buffer\[2\] = (unsigned char)(heading_radians >> 0);
&nbsp;&nbsp; &nbsp;//nmea2000_buffer\[3\] = (unsigned char)(heading_radians >> 8);
&nbsp;&nbsp; &nbsp;//nmea2000_buffer\[4\] = (unsigned char)(speed_metres >> 0);
&nbsp;&nbsp; &nbsp;//nmea2000_buffer\[5\] = (unsigned char)(speed_metres >> 8);
&nbsp;&nbsp; &nbsp;//nmea2000_buffer\[6\] = 0xff; // resv 8 bits
&nbsp;&nbsp; &nbsp;//nmea2000_buffer\[7\] = 0xff; // resv 8 bits
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //FIX ON OIKEASTI BOOL-TYYPPINEN JA KERTOO, SAAKO GPS YHTEYDEN SATELLIITTEIHIN, TS. ONKO TIETO LUOTETTAVAA

&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;
&nbsp;&nbsp; &nbsp;if(rx_id == 167248164)
&nbsp;&nbsp; &nbsp;{
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;viesti7 = rx_msg\[0\];
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;viesti8 = rx_msg\[1\];
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;viesti9 = rx_msg\[2\];
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;viesti10 = rx_msg\[3\];
&nbsp;&nbsp; &nbsp;}

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Serial.println(1, DEC);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Serial.println(2, DEC);
&nbsp;&nbsp; &nbsp;Serial.println(viesti1, DEC);
&nbsp;&nbsp; &nbsp;Serial.println(viesti2, DEC);
&nbsp;&nbsp; &nbsp;Serial.println(viesti3, DEC);
&nbsp;&nbsp; &nbsp;Serial.println(viesti4, DEC);
&nbsp;&nbsp; &nbsp;Serial.println(viesti5, DEC);
&nbsp;&nbsp; &nbsp;Serial.println(viesti6, DEC);
&nbsp;&nbsp; &nbsp;Serial.println(viesti7, DEC);
&nbsp;&nbsp; &nbsp;Serial.println(viesti8, DEC);
&nbsp;&nbsp; &nbsp;Serial.println(viesti9, DEC);
&nbsp;&nbsp; &nbsp;Serial.println(viesti10, DEC);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;

&nbsp;&nbsp; &nbsp;//DEBUG
&nbsp;&nbsp; &nbsp;//Serial.println(CAN_kaasu, BYTE);
&nbsp;&nbsp; &nbsp;//Serial.println(PWM_suunta, BYTE);
&nbsp;&nbsp; &nbsp;//Serial.println(PWM_kauha, BYTE);
&nbsp;&nbsp; &nbsp;//Serial.println(MoottoriOnOff, BYTE);

&nbsp;&nbsp; &nbsp;//tartteeko tätä?
&nbsp;&nbsp; &nbsp;//delay(10);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int Check1 = SWread();
&nbsp;&nbsp; &nbsp;CAN_kaasu = SWread(); // luetaan radiomodeemin ensimmäinen viesti = kaasu
&nbsp;&nbsp; &nbsp;PWM_suunta = SWread(); // luetaan radiomodeemin toinen viesti = suunta
&nbsp;&nbsp; &nbsp;PWM_kauha = SWread();&nbsp; // luetaan radiomodeemin kolmas viesti = kauha
&nbsp;&nbsp; &nbsp;MoottoriOnOff = SWread(); // // luetaan radiomodeemin neljäs viesti = moottorin ON/OFF tieto
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int Check2 = SWread();

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if(Check1==2 && Check2==3)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;// annetaan arvot pwm lähdöille
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;analogWrite(suuntaPin, PWM_suunta);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;analogWrite(kauhaPin, PWM_kauha);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;//Pitääkö MoottoriOnOff-viestiä pommittaa koko ajan ettei moottori sammu???
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;if(MoottoriOnOff \!= MoottoriVanha)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;if(MoottoriOnOff == 0x51) // Moottori päälle
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp; // lähetetään CAN-viesti moottori päälle
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp; candata_OnOff\[0\] = 0x01;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp; CAN.load_ff_0(8, id_OnOff, candata_OnOff);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp; CAN.send_0();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;else if(MoottoriOnOff == 0x50) // Moottori pois päältä
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp; // lähetetään CAN-viesti moottori pois päältä
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp; candata_OnOff\[0\] = 0x00;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp; CAN.load_ff_0(8, id_OnOff, candata_OnOff);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp; CAN.send_0();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;MoottoriVanha = MoottoriOnOff;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;//Pitääkö CAN-viestien välillä olla viive?
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;delay(10);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;// lasketaan radiomoodemilta saatu kaasun arvo oikeaan muotoon
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;// ja lähetetään se CAN-viestinä moottorille
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;kaasu = 0x8000-(12*(CAN_kaasu*10));
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;candata\[0\] = kaasu >> 8;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;candata\[1\] = kaasu & 0xFF;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;candata\[2\] = 0x00;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;CAN.load_ff_0(8, 0x65, candata); //65 = MOOTTORIN ID
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;CAN.send_0();

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }//CHECKIN LOPETUS
&nbsp;}

// funktio radiomodeemin lukemiseen
int SWread()
{
&nbsp;&nbsp; &nbsp;byte val = 0;

&nbsp;&nbsp; &nbsp;while (digitalRead(rx));

&nbsp;&nbsp; &nbsp;if (digitalRead(rx) == LOW) {
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;delayMicroseconds(Delay / 2 - clockCyclesToMicroseconds(50));
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;for(int offset = 0; offset < 8; offset++) {
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;delayMicroseconds(Delay);
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;val \|= digitalRead(rx) << offset;
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;}
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;//wait for stop bit + extra
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;delayMicroseconds(bitDelay);

&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;return val;
&nbsp;&nbsp; &nbsp;}
&nbsp; &nbsp;&nbsp; &nbsp;//return \-1;
}
  • No labels
You must log in to comment.