Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed ADS1015 USBDevice MCP4725
Revision 4:d29a7b77b1c6, committed 2014-04-07
- Comitter:
- jamesdem1123
- Date:
- Mon Apr 07 04:32:52 2014 +0000
- Parent:
- 3:a01ea06ca9ab
- Child:
- 5:6b90ed7f3eb1
- Commit message:
- working 1230
Changed in this revision
| main.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- a/main.cpp Tue Apr 01 14:47:21 2014 +0000
+++ b/main.cpp Mon Apr 07 04:32:52 2014 +0000
@@ -1,17 +1,33 @@
#include "mbed.h"
#include "Adafruit_ADS1015.h"
-#include "USBSerial.h"
+//#include "USBSerial.h"
-#define SERIAL_BAUD_RATE 9600
+#include <iostream>
+#include <map>
+#include <string>
+#include <stdio.h>
+
+//#define SERIAL_BAUD_RATE 9600
//library function setup
I2C i2c(p28, p27);
Adafruit_ADS1015 ads(&i2c);
-USBSerial pc; // USB CDC serial port
+//USBSerial pc; // USB CDC serial port
CAN cantbus(p30, p29);
Ticker graph;
+
+//can stuff
+Ticker ticker;
+CAN can1(p9, p10);
+CAN can2(p30, p29);
+char counter[8] = {1,1,1,1,1,1,1,1};
+int test=0x0;
+char counter2=0;
+
+
+
//leds
DigitalOut led0(LED1);
DigitalOut led1(LED2);
@@ -19,9 +35,9 @@
DigitalOut led3(LED4);
//current readings
-AnalogIn iBat(p15); //75a
+AnalogIn iBat(p15); //75a PROTO
AnalogIn iTwelve(p17); //31a
-AnalogIn iFive(p18);//31a
+AnalogIn iFive(p18);//31a PROTO
AnalogIn iMot(p16);//75a
AnalogIn iCpu(p20);//31a
AnalogIn iPeri(p19);//31a
@@ -33,7 +49,7 @@
#define fiveScal 12.2
//Relays
-DigitalOut relSens(p21);
+DigitalOut relSens(p21); // PROTO
DigitalOut relUsr(p22);
DigitalOut relNet(p23);
DigitalOut relCpuRst(p24);
@@ -41,88 +57,139 @@
//30 minute graph arrays
-uint16_t iBatArr [180];
-uint16_t vBatArr [180];
-uint16_t iTwelveArr [180];
-uint16_t vTwelveArr [180];
-uint16_t iFiveArr [180];
-uint16_t vFiveArr [180];
-uint16_t iMotArr [180];
-uint16_t vMotArr [180];
-uint16_t iCpuArr [180];
-uint16_t iPeriArr [180];
+uint16_t powerArr [10]; //{iBat,vBat,iTwelve, vTwelve, iFive, vFive, iMot, vMot, iCpu, iPeri}
+
+
+float ibat =0;
+float ifive = 0;
//volt
uint16_t vBat, vTwelve,vFive,vMot ;
+
+void sending_values(long int int1,long int int2,long int int3,long int int4, int ID )
+{
+ //val();
+ counter[0]=int1 >> 8;
+ counter[1]=int1 & 0xFF;
+ counter[2]=int2 >> 8;
+ counter[3]=int2 & 0xFF;
+ counter[4]=int3 >> 8;
+ counter[5]=int3 & 0xFF;
+ counter[6]=int4 >> 8;
+ counter[7]=int4 & 0xFF;
+
+ if(can1.write(CANMessage(ID, counter, 8))) {
+
+ printf("Message sent: %x%x\r\n", counter[1],counter[0]);
+ printf("Should be: %d, %d, %d, %d\r\n", int1,int2,int3,int4);
+}
+}
+
+
+
+
+
+
+void Can_main(bool send,long int val1,long int val2,long int val3,long int val4, int the_ID) {
+
+
+
+ std::map <int,string> CanIDMap;
+
+ //CanIDMap[0]="Motor Controller String";
+ CanIDMap[1]="Motor Controller Ints";
+ //CanIDMap[2]="Power Controller Voltage_Battery";
+ CanIDMap[3]="Power Controller Voltage_5V";
+ //CanIDMap[4]="Power Controller Voltage_12V";
+ CanIDMap[5]="Power Controller Voltage_Motor";
+ CanIDMap[6]="Power Controller Current_Battery";
+ CanIDMap[7]="Power Controller Current_5V";
+ CanIDMap[8]="Power Controller Current_12V";
+ CanIDMap[9]="Power Controller Current_Motor";
+ CanIDMap[10]="LCD Controller String";
+ CanIDMap[11]="LCD Controller Ints";
+ CanIDMap[12]="Zac is the Best";
+
+
+
+ //ticker.attach(&sending_values,1);
+ CANMessage msg;
+ int fixer1=0;
+ int fixer2=0;
+ int fixer3=0;
+ int fixer4=0;
+ while(1) {
+ if (send==true){
+ sending_values(val1,val2,val3,val4,the_ID);
+ }
+ if(can2.read(msg)) {
+ bool tester= CanIDMap.find(msg.id) != CanIDMap.end();
+ if (tester){
+ printf("Message received: %x %x \r\n", msg.data[0], msg.data[1]);
+
+ //printf("ID of message: %s \r\n", CanIDMap[msg.id]);
+
+ std::string str=CanIDMap[msg.id];
+ unsigned pos = str.find("_"); // position of "_" in str
+ std::string str3 = str.substr (pos+1); // get from past"_" to the end
+ // printf("Dealing with: %s \r\n",str3);
+ fixer1=((msg.data[0]<<8) | (msg.data[1]));
+ fixer2=((msg.data[2]<<8) | (msg.data[3]));
+ fixer3=((msg.data[4]<<8) | (msg.data[5]));
+ fixer4=((msg.data[6]<<8) | (msg.data[7]));
+ led2 = !led2;
+
+ printf("Resulting Answer back is: %d, %d, %d, %d\r\n\r\n\r\n",fixer1,fixer2,fixer3,fixer4 );
+ break;
+ }
+
+ else{
+
+ printf("Can Message, not related to this controller\r\n\r\n");
+
+ }
+
+ wait(0.2);
+ }
+
+
+}
+
+
+}
+
+
+
+
void updateArray (void)
{
- vBat = ads.readADC_SingleEnded(0); // read channel 0
+
+ printf("reading Vbat: %d\r\n", vBat); // print reading
+ printf("reading Battery Current: %2.8f\r\n", ibat); // print reading
+ printf("reading Five Volt Current: %2.8f\r\n", ifive); // print reading
+ Can_main(true,15,84,1254,3254,1);
+ // printf("reading: %d\r\n", vMot); // print reading
+ /*
+
+ vBat = ads.readADC_SingleEnded(0); // read channel 0 PROTO
vTwelve = ads.readADC_SingleEnded(1); // read channel 0
vFive = ads.readADC_SingleEnded(2); // read channel 0
vMot = ads.readADC_SingleEnded(3); // read channel 0
- for(int i =179; i >0; i--) {
- iBatArr[i] =iBatArr[i-1];
- }
- iBatArr[0]= iBat.read_u16();
-
- for(int i =179; i >0; i--) {
- vBatArr[i] =vBatArr[i-1];
- }
- vBatArr[0]= vBat;
-
- for(int i =179; i >0; i--) {
- iTwelveArr[i] =iTwelveArr[i-1];
- }
- iTwelveArr[0]= iTwelve.read_u16();
-
- for(int i =179; i >0; i--) {
- vTwelveArr[i] =vTwelveArr[i-1];
- }
- vTwelveArr[0]= vTwelve;
-
- for(int i =179; i >0; i--) {
- iFiveArr[i] =iFiveArr[i-1];
- }
- iFiveArr[0]= iFive.read_u16();
-
- for(int i =179; i >0; i--) {
- vFiveArr[i] =vFiveArr[i-1];
- }
- vFiveArr[0]= vFive;
-
- for(int i =179; i >0; i--) {
- iMotArr[i] =iMotArr[i-1];
- }
- iMotArr[0]= iMot.read_u16();
-
- for(int i =179; i >0; i--) {
- vMotArr[i] =vMotArr[i-1];
- }
- vMotArr[0]= vMot;
-
- for(int i =179; i >0; i--) {
- iCpuArr[i] =iCpuArr[i-1];
- }
- iCpuArr[0]= iCpu.read_u16();
-
- for(int i =179; i >0; i--) {
- iPeriArr[i] =iPeriArr[i-1];
- }
- iPeriArr[0]= iPeri.read_u16();
- led0= !led0;
+
+ */
}
void setup(void)
{
- graph.attach(&updateArray, 10); //update array every 10 seconds
- cantbus.frequency(500000);
- CANMessage can_MsgRx;
+ graph.attach(&updateArray, 10); //update array every 10 seconds
+ // cantbus.frequency(500000);
+ // CANMessage can_MsgRx;
//initialize relays
relSens= 0;
@@ -139,9 +206,9 @@
{
setup();
- graph.attach(updateArray, 1); //update array every 10 seconds
- cantbus.frequency(500000);
- CANMessage can_MsgRx;
+ graph.attach(updateArray, 2); //update array every 10 seconds
+ //cantbus.frequency(500000);
+ // CANMessage can_MsgRx;
//initialize relays
relSens= 0;
@@ -150,14 +217,14 @@
relCpuRst =0;
relUsr=0;
while (1) {
+vBat = ads.readADC_SingleEnded(0); // read channel 0
+ibat = (iBat.read())*3.3;
+ifive = (iFive.read())*3.3;
- pc.printf("reading: %d\r\n", vBat); // print reading
- pc.printf("reading: %d\r\n", vTwelve); // print reading
- pc.printf("reading: %d\r\n", vFive); // print reading
- pc.printf("reading: %d\r\n", vMot); // print reading
- wait(2);
+led0=!led0;
+
}
}
@@ -173,3 +240,10 @@
+
+
+
+
+
+
+