Skip to content

Commit

Permalink
Merge branch 'dev' of https://github.com/team178/ToF-CAN into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
ericsims committed Mar 20, 2019
2 parents 39ff5ab + 552567d commit d729be1
Show file tree
Hide file tree
Showing 21 changed files with 12,205 additions and 11,997 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,5 @@
*.exe
*.out
*.app

*.lck
Binary file modified firmware/ToF-CAN-Firmware/.vs/ToF-CAN-Firmware/v14/.atsuo
Binary file not shown.
Binary file modified firmware/ToF-CAN-Firmware/Release/ToF-CAN-Firmware.elf
Binary file not shown.
2,499 changes: 1,262 additions & 1,237 deletions firmware/ToF-CAN-Firmware/Release/ToF-CAN-Firmware.hex

Large diffs are not rendered by default.

18,398 changes: 9,268 additions & 9,130 deletions firmware/ToF-CAN-Firmware/Release/ToF-CAN-Firmware.lss

Large diffs are not rendered by default.

545 changes: 290 additions & 255 deletions firmware/ToF-CAN-Firmware/Release/ToF-CAN-Firmware.map

Large diffs are not rendered by default.

2,499 changes: 1,262 additions & 1,237 deletions firmware/ToF-CAN-Firmware/Release/ToF-CAN-Firmware.srec

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions firmware/ToF-CAN-Firmware/Release/default eeprom.hex
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
:020000002006D8
:00000001FF
12 changes: 6 additions & 6 deletions firmware/ToF-CAN-Firmware/libs/mcp2515/mcp2515.c
Original file line number Diff line number Diff line change
Expand Up @@ -142,22 +142,22 @@ bool mcp2515_init(void)
spi_putc(SPI_WRITE);
spi_putc(CNF3);

spi_putc((1<<PHSEG21)); // Bitrate 125 kbps at 16 MHz
spi_putc((1<<BTLMODE)|(1<<PHSEG11));
spi_putc((1<<BRP2)|(1<<BRP1)|(1<<BRP0));
spi_putc(0x04); // Bitrate 1 Mbps at 20 MHz
spi_putc(0x89);
spi_putc(0x00);

// activate interrupts
spi_putc((1<<RX1IE)|(1<<RX0IE));
SET(MCP2515_CS);

// test if we could read back the value => is the chip accessible?
if (mcp2515_read_register(CNF1) != ((1<<BRP2)|(1<<BRP1)|(1<<BRP0))) {
if (mcp2515_read_register(CNF1) != 0x00) {
return false;
}

mcp2515_write_register(CNF1, 0x04);
mcp2515_write_register(CNF1, 0x00);

// deaktivate the RXnBF Pins (High Impedance State)
// deactivate the RXnBF Pins (High Impedance State)
mcp2515_write_register(BFPCTRL, 0);

// set TXnRTS as inputs
Expand Down
151 changes: 111 additions & 40 deletions firmware/ToF-CAN-Firmware/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
*/


// TODO: do we need to switch to an external oscillator?
/*
* Fuses Set to:
* Low: 0xE2
Expand All @@ -21,13 +20,13 @@
#include <stdlib.h>
#include <avr/pgmspace.h>
#include <avr/interrupt.h>
#include <avr/eeprom.h>

#define TWBR TWBR0
#define TWCR TWCR0
#define TWSR TWSR0
#define TWDR TWDR0


#define SPDR SPDR0
#define SPCR SPCR0
#define SPSR SPSR0
Expand All @@ -43,7 +42,48 @@
#define PIN_LED_G DDD5
#define PIN_LED_R DDD7

#define CAN_BUS_ADDR 0x620
#define DEFAULT_CAN_BUS_ADDR 0x620
#define EEPROM_ADDR_LOCATION 0x0000

#define ERROR_NONE 0
#define ERROR_OUT_OF_RANGE 1
#define ERROR_WRITING_TO_CAN 2
#define ERROR_INIT_CAN 3
#define ERROR_INIT_VL53L0X 3

#define COLOR_OFF 0
#define COLOR_RED 1
#define COLOR_GREEN 2
#define COLOR_BLUE 3

int errorCode = ERROR_NONE;
uint32_t canBusAddr;


void setColor(int color) {
switch(color){
case COLOR_RED:
PORTD &= ~(1<<PIN_LED_G);
PORTD &= ~(1<<PIN_LED_B);
PORTD |= (1<<PIN_LED_R);
break;
case COLOR_GREEN:
PORTD &= ~(1<<PIN_LED_R);
PORTD &= ~(1<<PIN_LED_B);
PORTD |= (1<<PIN_LED_G);
break;
case COLOR_BLUE:
PORTD &= ~(1<<PIN_LED_R);
PORTD &= ~(1<<PIN_LED_G);
PORTD |= (1<<PIN_LED_B);
break;
default:
PORTD &= ~(1<<PIN_LED_R);
PORTD &= ~(1<<PIN_LED_G);
PORTD &= ~(1<<PIN_LED_B);
break;
}
}

void init_uart(uint16_t baudrate) {

Expand Down Expand Up @@ -79,11 +119,11 @@ void print_can_message(tCAN *message)
uint8_t length = message->header.length;

printf("id: 0x%3x\r\n", message->id);
printf("laenge: %d\r\n", length);
printf("length: %d\r\n", length);
printf("rtr: %d\r\n", message->header.rtr);

if (!message->header.rtr) {
printf("daten: ");
printf("data: ");

for (uint8_t i = 0; i < length; i++) {
printf("0x%02x ", message->data[i]);
Expand All @@ -94,8 +134,8 @@ void print_can_message(tCAN *message)

Adafruit_VL53L0X tof = Adafruit_VL53L0X();

char buffer[16];
uint16_t count = 0;
uint8_t count = 0;

int main(void)
{
static FILE uart_stdout;
Expand All @@ -105,66 +145,97 @@ int main(void)
DDRD |= (1<<PIN_LED_B);
DDRD |= (1<<PIN_LED_G);
DDRD |= (1<<PIN_LED_R);

setColor(COLOR_RED);

init_uart(9600);
_delay_us(200);
_delay_ms(500);

//sei();
if (!mcp2515_init()) {
uart_puts("Can't communicate with MCP2515!\r\n");
errorCode = ERROR_INIT_CAN;
for (;;);
}
else {
uart_puts("MCP2515 is active\r\n");
}
i2c_init();
tof.begin();
if(!tof.begin()){
uart_puts("Can't connect to VL53L0X!");
errorCode = ERROR_INIT_VL53L0X;
}

uint32_t newAddr = eeprom_read_word(EEPROM_ADDR_LOCATION);
if(newAddr >= 0x0620 && newAddr <0x1000) {
printf("Got CAN address in EEPROM: 0x%04x!\r\n", newAddr);
canBusAddr = newAddr;
} else {
printf("Bad CAN address in EEPROM, defaulting to 0x%04x!\r\n", DEFAULT_CAN_BUS_ADDR);
}

_delay_ms(500);

VL53L0X_RangingMeasurementData_t measure;
tCAN message;

message.id = CAN_BUS_ADDR;
message.id = canBusAddr;
message.header.rtr = 0;
message.header.length = 2;
message.data[0] = 0xab;
message.data[1] = 0xcd;
message.header.length = 3;
message.data[0] = 0x00; // byte error code
message.data[1] = 0x00; // byte H
message.data[2] = 0x00; // byte L

//printf("\r\nwechsle zum Loopback-Modus\r\n");
//mcp2515_bit_modify(CANCTRL, (1<<REQOP2)|(1<<REQOP1)|(1<<REQOP0), (1<<REQOP1));

mcp2515_bit_modify(CANCTRL, (1<<REQOP2)|(1<<REQOP1)|(1<<REQOP0), 0);
printf("CNF1: 0x%02x\r\n", mcp2515_read_register(CNF1));
while (1)
{
if(errorCode != ERROR_INIT_CAN && errorCode != ERROR_INIT_VL53L0X) {
tof.rangingTest(&measure, false);
if (measure.RangeStatus != 4) {
uint16_t distance = measure.RangeMilliMeter;
if(distance < 8191) {
printf("Distance (mm): %u, range status:%d\r\n", distance, measure.RangeStatus);

message.data[1] = (distance>>8) & 0xFF;
message.data[2] = (distance) & 0xFF;

errorCode = ERROR_NONE;
} else {
// sometimes we get a bad reading of 8191 or 8192?
errorCode = ERROR_OUT_OF_RANGE;
}
} else {
// OUT OF RANGE
errorCode = ERROR_OUT_OF_RANGE;
}
}


tof.rangingTest(&measure, false);
if (measure.RangeStatus != 4) {
uint16_t distance = measure.RangeMilliMeter;
printf("Distance (mm): %ld", distance);
uart_puts(buffer);
uart_puts("\r\n");

message.data[0]= (distance>>8) && 0xFF;
message.data[1]= (distance) && 0xFF;

if (mcp2515_send_message(&message)) {
uart_puts("sent them bytes");
message.data[0] = errorCode;

if (mcp2515_send_message(&message)) {
//uart_puts("sent them bytes\r\n");
} else {
uart_puts("Error writing message to can bus!\r\n");
print_can_message(&message);
errorCode = ERROR_WRITING_TO_CAN;
}

_delay_ms(15);
if(errorCode == ERROR_NONE) {
setColor(COLOR_GREEN);
} else if(errorCode == ERROR_OUT_OF_RANGE) {
setColor(COLOR_BLUE);
} else if(errorCode == ERROR_WRITING_TO_CAN) {
if(count<3) {
setColor(COLOR_RED);
} else {
uart_puts("Error writing message to can bus!\r\n");
print_can_message(&message);
setColor(COLOR_OFF);
}

} else {
// OUT OF RANGE
setColor(COLOR_RED);
}

PORTD |= (1<<PIN_LED_B);
_delay_ms(1);
PORTD &= ~(1<<PIN_LED_B);
_delay_ms(999);
count++;
if(++count>4) count = 0;
}
}

Binary file modified hardware/CAD/TOF Assembly.iam
Binary file not shown.
Binary file modified hardware/CAD/TOF Case P1.ipt
Binary file not shown.
Binary file modified hardware/CAD/TOF Case P1.stl
Binary file not shown.
Binary file modified hardware/CAD/TOF Case P2.ipt
Binary file not shown.
Binary file modified hardware/CAD/TOF Case P2_ISO.ipt
Binary file not shown.
Binary file modified hardware/CAD/TOF Case P2_ISO.stl
Binary file not shown.
38 changes: 1 addition & 37 deletions hardware/CAN TOF.brd
Original file line number Diff line number Diff line change
Expand Up @@ -2228,7 +2228,7 @@ design rules under a new name.</description>
<attribute name="SPICEPREFIX" value="R" x="-2.54" y="33.02" size="1.778" layer="27" display="off"/>
<attribute name="VALUE" x="14.605" y="36.83" size="1.27" layer="27"/>
</element>
<element name="Q1" library="crystal" library_urn="urn:adsk.eagle:library:204" package="CTS406" package3d_urn="urn:adsk.eagle:package:12089/1" value="8MHz" x="27.94" y="31.75" smashed="yes">
<element name="Q1" library="crystal" library_urn="urn:adsk.eagle:library:204" package="CTS406" package3d_urn="urn:adsk.eagle:package:12089/1" value="20MHz" x="27.94" y="31.75" smashed="yes">
<attribute name="MF" value="" x="3.81" y="7.62" size="1.778" layer="27" display="off"/>
<attribute name="MPN" value="" x="3.81" y="7.62" size="1.778" layer="27" display="off"/>
<attribute name="NAME" x="25.4" y="34.29" size="0.8128" layer="25"/>
Expand Down Expand Up @@ -2284,24 +2284,6 @@ design rules under a new name.</description>
<attribute name="SPICEPREFIX" value="R" x="29.845" y="11.43" size="1.778" layer="27" rot="R270" display="off"/>
<attribute name="VALUE" x="27.305" y="12.065" size="1.27" layer="27" rot="R270"/>
</element>
<element name="Q2" library="crystal" library_urn="urn:adsk.eagle:library:204" package="CTS406" package3d_urn="urn:adsk.eagle:package:12089/1" value="16MHz" x="3.81" y="-5.08" smashed="yes">
<attribute name="MF" value="" x="-5.08" y="5.08" size="1.778" layer="27" display="off"/>
<attribute name="MPN" value="" x="-5.08" y="5.08" size="1.778" layer="27" display="off"/>
<attribute name="NAME" x="1.27" y="-2.54" size="1.27" layer="25"/>
<attribute name="OC_FARNELL" value="unknown" x="-5.08" y="5.08" size="1.778" layer="27" display="off"/>
<attribute name="OC_NEWARK" value="unknown" x="-5.08" y="5.08" size="1.778" layer="27" display="off"/>
<attribute name="VALUE" x="1.27" y="-8.89" size="1.27" layer="27"/>
</element>
<element name="C8" library="rcl" library_urn="urn:adsk.eagle:library:334" package="C0402" package3d_urn="urn:adsk.eagle:package:23626/2" value="20pF" x="1.27" y="-12.7" smashed="yes">
<attribute name="NAME" x="0.635" y="-12.065" size="1.27" layer="25"/>
<attribute name="SPICEPREFIX" value="C" x="-6.35" y="-3.81" size="1.778" layer="27" display="off"/>
<attribute name="VALUE" x="0.635" y="-14.605" size="1.27" layer="27"/>
</element>
<element name="C9" library="rcl" library_urn="urn:adsk.eagle:library:334" package="C0402" package3d_urn="urn:adsk.eagle:package:23626/2" value="20pF" x="1.27" y="-17.78" smashed="yes">
<attribute name="NAME" x="0.635" y="-17.145" size="1.27" layer="25"/>
<attribute name="SPICEPREFIX" value="C" x="-6.35" y="-8.89" size="1.778" layer="27" display="off"/>
<attribute name="VALUE" x="0.635" y="-19.685" size="1.27" layer="27"/>
</element>
</elements>
<signals>
<signal name="+5V">
Expand Down Expand Up @@ -2532,10 +2514,6 @@ design rules under a new name.</description>
<vertex x="0" y="0"/>
<vertex x="33.02" y="0"/>
</polygon>
<contactref element="C8" pad="2"/>
<contactref element="C9" pad="2"/>
<wire x1="1.92" y1="-12.7" x2="1.92" y2="-17.78" width="0" layer="19" extent="1-1"/>
<wire x1="7.62" y1="4.445" x2="1.92" y2="-12.7" width="0" layer="19" extent="1-16"/>
</signal>
<signal name="INT0">
<contactref element="U1" pad="32"/>
Expand Down Expand Up @@ -3028,20 +3006,6 @@ design rules under a new name.</description>
<wire x1="2.54" y1="12.85875" x2="2.54" y2="12.905" width="0.2032" layer="1"/>
<via x="3.96875" y="11.90625" extent="1-16" drill="0.35"/>
</signal>
<signal name="N$12">
<contactref element="C8" pad="1"/>
<contactref element="Q2" pad="1"/>
<contactref element="U1" pad="8"/>
<wire x1="1.61" y1="-6.28" x2="14.4526" y2="32.76" width="0" layer="19" extent="1-16"/>
<wire x1="0.62" y1="-12.7" x2="1.61" y2="-6.28" width="0" layer="19" extent="1-1"/>
</signal>
<signal name="N$13">
<contactref element="C9" pad="1"/>
<contactref element="Q2" pad="3"/>
<contactref element="U1" pad="7"/>
<wire x1="6.01" y1="-3.88" x2="14.4526" y2="33.56" width="0" layer="19" extent="1-16"/>
<wire x1="0.62" y1="-17.78" x2="6.01" y2="-3.88" width="0" layer="19" extent="1-1"/>
</signal>
</signals>
<mfgpreviewcolors>
<mfgpreviewcolor name="soldermaskcolor" color="0xC8008000"/>
Expand Down
Loading

0 comments on commit d729be1

Please sign in to comment.