#include <SPI.h> #include <SoftwareSerial.h> #include <CAN.h> #define BAUDRATE 125 //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; unsigned long id_vertaa=0; byte viesti1=0; byte viesti2=0; byte viesti3=0; byte viesti4=0; byte viesti5=0; byte viesti6=0; byte viesti; //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 _bitPeriod; int bitDelay; int ledPin = 6; int ledPin2 = 9; void setup() { //CAN setup CAN.begin(); //init CANClass, which also initializes SPI communication to CAN tranceiver CAN.baudConfig(BAUDRATE); //set baudrate 250kbit/s CAN.setMode(NORMAL); //set mode for CANClass candata[0]=0x00; candata[1]=0x00; candata[2]=0x00; candata[3]=0x00; candata[4]=0x00; candata[5]=0x00; candata[6]=0x00; candata[7]=0x00; candata_OnOff[0]=0x00; candata_OnOff[1]=0x00; candata_OnOff[2]=0x00; candata_OnOff[3]=0x00; candata_OnOff[4]=0x00; candata_OnOff[5]=0x00; candata_OnOff[6]=0x00; candata_OnOff[7]=0x00; Serial.begin(9600); //initialize serial communication pinMode(ledPin, OUTPUT); pinMode(ledPin2, OUTPUT); //RS232 setup pinMode(rx,INPUT); pinMode(tx,OUTPUT); digitalWrite(tx,HIGH); _bitPeriod=1000000 / 9600; bitDelay = _bitPeriod - clockCyclesToMicroseconds(50); delayMicroseconds(_bitPeriod); } void loop() { CAN.readDATA_ff_1(&rx_len, rx_msg, &rx_id); id_vertaa=rx_id, HEX; if(rx_id==0x00000066) { viesti1=rx_msg[0]; viesti2=rx_msg[1]; viesti3=rx_msg[2]; viesti4=rx_msg[3]; viesti5=rx_msg[4]; viesti6=rx_msg[5]; } //TÄNNE GEPSIN LUKEMINEN. LISÄÄ MYÖS GEPSITIETOJEN LÄHETYS (VIESTI7...) //heading_radians = (unsigned int)(radian(heading)*10000); //speed_metres = (unsigned int)(speed*0.5144*100); //nmea2000_buffer[0] = (unsigned char)(fix); // position fix //nmea2000_buffer[1] = 0xfc; // 11111100 True heading 00 + resv 6 bits //nmea2000_buffer[2] = (unsigned char)(heading_radians >> 0); //nmea2000_buffer[3] = (unsigned char)(heading_radians >> 8); //nmea2000_buffer[4] = (unsigned char)(speed_metres >> 0); //nmea2000_buffer[5] = (unsigned char)(speed_metres >> 8); //nmea2000_buffer[6] = 0xff; // resv 8 bits //nmea2000_buffer[7] = 0xff; // resv 8 bits //FIX ON OIKEASTI BOOL-TYYPPINEN JA KERTOO, SAAKO GPS YHTEYDEN SATELLIITTEIHIN, TS. ONKO TIETO LUOTETTAVAA //uncommentaa seuraavat /* if(rx_id==ID_GPS) { viesti7=rx_msg[2]; viesti8=rx_msg[3]; viesti9=rx_msg[4]; viesti10=rx_msg[5]; }*/ delay(10); CAN_kaasu=SWread(); PWM_suunta=SWread(); PWM_kauha=SWread(); MoottoriOnOff=SWread(); Serial.println(viesti1, BYTE); Serial.println(viesti2, BYTE); Serial.println(viesti3, BYTE); Serial.println(viesti4, BYTE); Serial.println(viesti5, BYTE); Serial.println(viesti6, BYTE); analogWrite(ledPin, PWM_suunta); analogWrite(ledPin2, PWM_kauha); //Pitääkö MoottoriOnOff-viestiä pommittaa koko ajan ettei moottori sammu??? if(MoottoriOnOff!=MoottoriVanha) { if (MoottoriOnOff==0x51) // Moottori päälle { candata_OnOff[0]=0x01; CAN.load_ff_0(8,id_OnOff,candata_OnOff); CAN.send_0(); } else if (MoottoriOnOff==0x50) // Moottori pois päältä { candata_OnOff[0]=0x00; CAN.load_ff_0(8,id_OnOff,candata_OnOff); CAN.send_0(); } MoottoriVanha=MoottoriOnOff; //Pitääkö CAN-viestien välillä olla viive? delay(10); } Kaasu=0x8000-(12*(CAN_kaasu*10)); candata[0]=Kaasu>>8; candata[1]=Kaasu & 0xFF; candata[2]=0x00; CAN.load_ff_0(8,id_Kaasu,candata); CAN.send_0(); } void SWprint(int data) { byte mask; digitalWrite(tx,LOW); delayMicroseconds(bitDelay); for (mask = 0x01; mask>0; mask <<= 1) { if (data & mask){ // choose bit digitalWrite(tx,HIGH); // send 1 } else{ digitalWrite(tx,LOW); // send 0 } delayMicroseconds(bitDelay); } //stop bit digitalWrite(tx, HIGH); delayMicroseconds(bitDelay); } int SWread() { byte val = 0; // int bitDelay = _bitPeriod - clockCyclesToMicroseconds(50); while (digitalRead(rx)); //wait for start bit if (digitalRead(rx) == LOW) { delayMicroseconds(bitDelay / 2 - clockCyclesToMicroseconds(50)); for (int offset = 0; offset < 8; offset++) { delayMicroseconds(bitDelay); val |= digitalRead(rx) << offset; } //wait for stop bit + extra delayMicroseconds(_bitPeriod); return val; } //return -1; }