Components and supplies
Grove starter kit plus for Intel Edison
Arduino UNO
Winstar CAN BUS TFT 5" display
USB-A to Micro-USB Cable
Jumper wires (generic)
Project description
Code
Vehicle CAN BUS
arduino
1#include <cppQueue.h> 2 3// demo: CAN-BUS Shield, send data 4// loovee@seeed.cc 5#include <SPI.h> 6#include <can-serial.h> 7#include <mcp2515_can.h> 8#include <mcp2515_can_dfs.h> 9#include <mcp2518fd_can.h> 10#include <mcp2518fd_can_dfs.h> 11#include <mcp_can.h> 12#include <math.h> 13 14#define ROTARY_ANGLE_SENSOR A0 15#define ADC_REF 5 //reference voltage of ADC is 5v.If the Vcc switch on the seeeduino 16 //board switches to 3V3, the ADC_REF should be 3.3 17#define GROVE_VCC 5 //VCC of the grove interface is normally 5v 18#define FULL_ANGLE 100 //full value of the rotary angle is 300 degrees 19#define CAN_2515 20#define ON 1 21#define OFF 0 22#define MAX_LENGTH 100 23 24// Set SPI CS Pin according to your hardware 25 26#if defined(SEEED_WIO_TERMINAL) && defined(CAN_2518FD) 27// For Wio Terminal w/ MCP2518FD RPi Hat 28// Channel 0 SPI_CS Pin: BCM 8 29// Channel 1 SPI_CS Pin: BCM 7 30// Interupt Pin: BCM25 31const int SPI_CS_PIN = BCM8; 32const int CAN_INT_PIN = BCM25; 33#else 34 35// For Arduino MCP2515 Hat: 36// the cs pin of the version after v1.1 is default to D9 37// v0.9b and v1.0 is default D10 38const int SPI_CS_PIN = 9; 39const int CAN_INT_PIN = 2; 40#endif 41 42#ifdef CAN_2518FD 43#include "mcp2518fd_can.h" 44mcp2518fd CAN(SPI_CS_PIN); // Set CS pin 45#endif 46 47#ifdef CAN_2515 48#include "mcp2515_can.h" 49mcp2515_can CAN(SPI_CS_PIN); // Set CS pin 50#endif 51 52const int B = 4275; // B value of the thermistor 53const int R0 = 100000; // R0 = 100k 54const int pinTempSensor = SCL; // Grove - Temperature Sensor connect to SCL 55//const int vibrationSensor = SCL; 56const int Buzzer = 6; 57unsigned long startMillis; //some global variables available anywhere in the program 58unsigned long currentMillis; 59const unsigned long period = 100; 60float lastDegrees; 61float lastTemp; 62int present_condition = 0; 63int previous_condition = 0; 64unsigned char flagRecv = 0; 65unsigned char guage[8] = {0, 0, 0, 0, 0, 0, 0, 0}; 66unsigned char temp[8] = {0, 0, 0, 0, 0, 0, 0, 0}; 67 68unsigned char button[8]; 69 70unsigned char len = 0; 71 72typedef struct { 73 74 unsigned long id; 75 unsigned char toggle[8]; 76 77}CanBusPacket_t; 78 79CanBusPacket_t canBusPacket; 80CanBusPacket_t canBusPacketTest; 81unsigned char cnt = 0; 82cppQueue q(sizeof(CanBusPacket_t), MAX_LENGTH, FIFO, true); 83 84 85void setup() { 86 87 SERIAL_PORT_MONITOR.begin(115200); 88 pinMode(ROTARY_ANGLE_SENSOR, INPUT); 89 //pinMode(vibrationSensor, INPUT); 90 pinMode(pinTempSensor, INPUT); 91 pinMode(Buzzer, OUTPUT); 92 93 while(!Serial){}; 94 attachInterrupt(digitalPinToInterrupt(CAN_INT_PIN), MCP2515_ISR, FALLING); // start interrupt 95 96 while (CAN_OK != CAN.begin(CAN_250KBPS)) { // init can bus : baudrate = 500k 97 SERIAL_PORT_MONITOR.println("CAN init fail, retry..."); 98 delay(100); 99 } 100 SERIAL_PORT_MONITOR.println("CAN init ok!"); 101 startMillis = millis(); //initial start time 102 CAN.init_Mask(0, 0, 0x7ff); // there are 2 mask in mcp2515, you need to set both of them 103 CAN.init_Mask(1, 0, 0x7ff); 104 CAN.init_Filt(0, 0, 0x601); // there are 6 filter in mcp2515 105} 106 107void MCP2515_ISR() { 108 109 if (CAN_MSGAVAIL == CAN.checkReceive()) { 110 111 unsigned char toggle[8]; 112 113 CAN.readMsgBuf(&len, toggle); 114 115 canBusPacket.id = CAN.getCanId(); 116 117 canBusPacket.toggle[0] = toggle[0]; 118 canBusPacket.toggle[1] = toggle[1]; 119 canBusPacket.toggle[2] = toggle[2]; 120 canBusPacket.toggle[3] = toggle[3]; 121 canBusPacket.toggle[4] = toggle[4]; 122 canBusPacket.toggle[5] = toggle[5]; 123 canBusPacket.toggle[6] = toggle[6]; 124 canBusPacket.toggle[7] = toggle[7]; 125 126 q.push(&canBusPacket); // Push char to cppQueue 127 } 128} 129 130void loop() { 131 132 // send data: id = 0x00, standrad frame, data len = 8, stmp: data buf 133 float voltage; 134 float temperature; 135 float degrees; 136 int sensor_value; 137 int a; 138 float R; 139 int present_graph; 140 currentMillis = millis(); 141 previous_condition = present_condition; 142 143 144 if ((currentMillis - startMillis) >= period) { 145 146 sensor_value = analogRead(ROTARY_ANGLE_SENSOR); 147 voltage = (float)sensor_value*ADC_REF/1023; 148 degrees = 100 - ((voltage*FULL_ANGLE)/GROVE_VCC); 149 150 a = analogRead(pinTempSensor); 151 R = 1023.0/a-1.0; 152 R = R0*R; 153 temperature = 1.0/(log(R/R0)/B+1/298.15)-273.15; // convert to temperature via datasheet 154 155 // present_condition = 1- (digitalRead(vibrationSensor)); 156 // present_graph = 100 * present_condition; 157 158 startMillis = currentMillis; 159 unsigned char guage[8] = {0x2B, 0x02, 0x20, 0x07, degrees, 0x00, 0x00, 0x00}; 160 unsigned char temp[8] = {0x2B, 0x00, 0x20, 0x07, temperature, 0x00, 0x00, 0x00}; 161 //unsigned char indicator[8] = {0x2B, 0x05, 0x20, 0x07, present_graph, 0x00, 0x00, 0x00}; 162 163 if(lastDegrees != degrees){ 164 CAN.sendMsgBuf(0x67B, 0, 8, guage); 165 lastDegrees = degrees; 166 } 167 // if (previous_condition != present_condition) { 168 // CAN.sendMsgBuf(0x67B, 0, 8, indicator); 169 // previous_condition = present_condition; 170 // } 171 if(lastTemp != temperature){ 172 CAN.sendMsgBuf(0x67B, 0, 8, temp); 173 lastTemp = temperature; 174 } 175 } 176 177 if (!q.isEmpty()) { // Only if q is not empty 178 179 q.pop(&canBusPacketTest); 180 181 if ( canBusPacketTest.toggle[4] == 0x01 && 182 canBusPacketTest.toggle[0] == 0x2B && 183 canBusPacketTest.toggle[1]==0x04 && 184 canBusPacketTest.toggle[2]==0x20 && 185 canBusPacketTest.toggle[3]==0x08) { 186 187 digitalWrite(6, HIGH); 188 delay(5); 189 digitalWrite(6, LOW); 190 delay(5); 191 } 192 193 // for(int i = 0; i<len; i++) { // print the data 194 // Serial.print(canBusPacketTest[i], HEX); 195 // Serial.print("\ "); 196 // } 197 198 // if ( button[4] == 0x01 && button[0] == 0x2B && button[1]==0x03 && button[2]==0x20 && button[3]==0x07) { 199 // digitalWrite(13, ON); 200 // delay(1000); 201 // digitalWrite(13, OFF); 202 // delay(1000); 203 // for(int i = 0; i<len; i++) { // print the data 204 // Serial.print(button[i], HEX); 205 // Serial.print("\ "); 206 // } 207 // } 208 // Serial.print("\ 209"); 210 } 211 212} 213 214// END FILE 215
Comments
Only logged in users can leave comments
kenliao
0 Followers
•0 Projects
0