![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
This will generate the message for the programme
Dependencies: MODSERIAL Nanopb
main.cpp
- Committer:
- omatthews
- Date:
- 2019-08-27
- Revision:
- 0:388a2a9f5247
- Child:
- 1:fa8f59ecc52a
File content as of revision 0:388a2a9f5247:
#include "mbed.h" #include "pb.h" #include "pb_decode.h" #include "pb_encode.h" #include "memspcr.pb.h" #include "MODSERIAL.h" #include <vector> #include <iterator> DigitalOut led1(LED1); MODSERIAL pc(USBTX, USBRX, 512); uint8_t buffer[512]; size_t message_length; bool status; int i = 0; unsigned int c; memspcr_ExperimentConfiguration exp_config = memspcr_ExperimentConfiguration_init_zero; int buffer_length; int n_points; std::vector<memspcr_ThermalStep> profile; //Parameters float resistance[5] = {0.4,0.5,0.4,0.7,0.5}; int elapsed_time_ms[5] = {500,1000,1500,2500,3000}; int camera_offset_ms[5] = {50,250,0,100,100}; void write_message() { pc.printf("%d ",message_length); wait_ms(20); for (int i = 0; i < message_length; i++) { pc.printf("%02X ",buffer[i]); } } void encode_message() { pb_ostream_t ostream = pb_ostream_from_buffer(buffer, sizeof(buffer)); status = pb_encode(&ostream, memspcr_ExperimentConfiguration_fields, &exp_config); message_length = ostream.bytes_written; if (!status) { pc.printf("Encoding failed: %s\n", PB_GET_ERROR(&ostream)); } } bool encode_callback(pb_ostream_t *stream, const pb_field_t *field, void * const *arg) { vector <memspcr_ThermalStep> * elements = (vector <memspcr_ThermalStep> *)(*arg); vector <memspcr_ThermalStep>::iterator it; for (it = elements->begin(); it < elements->end(); it ++) { if (!pb_encode_tag_for_field(stream, field)) { printf("Encode callback error: %s\n", PB_GET_ERROR(stream)); return false; } if(!pb_encode_submessage(stream, memspcr_ThermalStep_fields, &(*it))) return false; } return true; } int main() { /* This is the buffer where we will store our message. */ pc.baud(115200); n_points = sizeof(resistance)/sizeof(float); buffer_length = sizeof(buffer)/sizeof(uint8_t); //Prepare message for (i = 0; i < n_points; i++) { memspcr_ThermalStep step = memspcr_ThermalStep_init_zero; step.resistance = resistance[i]; step.elapsed_time_ms = elapsed_time_ms[i]; step.camera_offset_ms = camera_offset_ms[i]; profile.push_back(step); } //Set set points exp_config.thermal.profile.arg = &profile; exp_config.thermal.profile.funcs.encode = encode_callback; exp_config.thermal.guard_drive_ratio = 0.26; exp_config.thermal.selected_heater = memspcr_ThermalConfiguration_Heater_MAIN; exp_config.thermal.adc_settling_time_us = 60; exp_config.thermal.pid_kp = 6.0; exp_config.thermal.pid_integral_time = 0.5; exp_config.thermal.control_loop_interval = 3000; exp_config.optics.pre_trigger_ms = 1; exp_config.optics.on_time_ms = 1; exp_config.optics.led_pwm = 0.5; exp_config.fluidics.pressure_setpoint = 0.4; exp_config.fluidics.pressure_hysterisis = 0.01; exp_config.logging_interval_ms = 50; encode_message(); write_message(); return 0; }